Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfa9208b

Browse files
add one more solution for 1252
1 parente6e57b0 commitfa9208b

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30-
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java)| O(m*n)| O(1)||Easy||
30+
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java)| O(m*n + k)| O(m*n)||Easy||
3131
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java)| O(n)| O(1)||Easy||
3232
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java)| O(n)| O(m)||Easy||
3333
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java)| O(1)| O(1)||Easy||

‎src/main/java/com/fishercoder/solutions/_1252.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
* */
3535
publicclass_1252 {
3636
publicstaticclassSolution1 {
37+
/**
38+
* Time: O(m*n + k) where k is the length of indices
39+
* Space: O(m*n)
40+
* */
3741
publicintoddCells(intn,intm,int[][]indices) {
3842
int[][]matrix =newint[n][m];
3943
for (inti =0;i <indices.length;i++) {
@@ -63,4 +67,26 @@ private void addOneToRow(int[][] matrix, int rowIndex) {
6367
}
6468
}
6569
}
70+
71+
publicstaticclassSolution2 {
72+
/**
73+
* Time: O(m*n + k) where k is the length of indices
74+
* Space: O(m + n)
75+
*/
76+
publicintoddCells(intn,intm,int[][]indices) {
77+
boolean[]row =newboolean[n];
78+
boolean[]column =newboolean[m];
79+
for (int[]index :indices) {
80+
row[index[0]] ^=true;
81+
column[index[1]] ^=true;
82+
}
83+
intoddNumberCount =0;
84+
for (inti =0;i <n;i++) {
85+
for (intj =0;j <m;j++) {
86+
oddNumberCount +=row[i] ^column[j] ?1 :0;
87+
}
88+
}
89+
returnoddNumberCount;
90+
}
91+
}
6692
}

‎src/test/java/com/fishercoder/_1252Test.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
publicclass_1252Test {
1010
privatestatic_1252.Solution1solution1;
11+
privatestatic_1252.Solution2solution2;
1112
privatestaticint[][]indices;
1213

1314
@BeforeClass
1415
publicstaticvoidsetup() {
1516
solution1 =new_1252.Solution1();
17+
solution2 =new_1252.Solution2();
1618
}
1719

1820
@Test
@@ -33,4 +35,22 @@ public void test2() {
3335
assertEquals(0,solution1.oddCells(2,2,indices));
3436
}
3537

38+
@Test
39+
publicvoidtest3() {
40+
indices =newint[][]{
41+
{0,1},
42+
{1,1}
43+
};
44+
assertEquals(6,solution2.oddCells(2,3,indices));
45+
}
46+
47+
@Test
48+
publicvoidtest4() {
49+
indices =newint[][]{
50+
{1,1},
51+
{0,0}
52+
};
53+
assertEquals(0,solution2.oddCells(2,2,indices));
54+
}
55+
3656
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp