Skip to content

Commit 3ef6e92

Browse files
add 1582
1 parent 8190b30 commit 3ef6e92

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1582|[Special Positions in a Binary Matrix](https://leetcode.com/problems/special-positions-in-a-binary-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1582.java) ||Array|Easy|
1112
|1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1576.java) |[:tv:](https://youtu.be/SJBDLYqrIsM)|String|Easy|
1213
|1574|[Shortest Subarray to be Removed to Make Array Sorted](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1574.java) ||Array, Binary Search|Medium|
1314
|1572|[Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1572.java) ||Array|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1582 {
4+
public static class Solution1 {
5+
public int numSpecial(int[][] mat) {
6+
int count = 0;
7+
for (int i = 0; i < mat.length; i++) {
8+
for (int j = 0; j < mat[0].length; j++) {
9+
if (mat[i][j] == 1 && isSpecial(mat, i, j)) {
10+
count++;
11+
}
12+
}
13+
}
14+
return count;
15+
}
16+
17+
private boolean isSpecial(int[][] mat, int row, int col) {
18+
for (int i = 0; i < mat.length; i++) {
19+
if (i != row && mat[i][col] == 1) {
20+
return false;
21+
}
22+
}
23+
for (int j = 0; j < mat[0].length; j++) {
24+
if (j != col && mat[row][j] == 1) {
25+
return false;
26+
}
27+
}
28+
return true;
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1582;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1582Test {
10+
private static _1582.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1582.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.numSpecial(new int[][]{
20+
{0, 0, 0, 0, 0},
21+
{1, 0, 0, 0, 0},
22+
{0, 1, 0, 0, 0},
23+
{0, 0, 1, 0, 0},
24+
{0, 0, 0, 1, 1}
25+
}));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
assertEquals(2, solution1.numSpecial(new int[][]{
31+
{0, 0, 0, 1},
32+
{1, 0, 0, 0},
33+
{0, 1, 1, 0},
34+
{0, 0, 0, 0}
35+
}));
36+
}
37+
38+
@Test
39+
public void test3() {
40+
assertEquals(3, solution1.numSpecial(new int[][]{
41+
{1, 0, 0},
42+
{0, 1, 0},
43+
{0, 0, 1}
44+
}));
45+
}
46+
47+
@Test
48+
public void test4() {
49+
assertEquals(1, solution1.numSpecial(new int[][]{
50+
{1, 0, 0},
51+
{0, 0, 1},
52+
{1, 0, 0}
53+
}));
54+
}
55+
56+
}

0 commit comments

Comments
 (0)