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

Commite6e57b0

Browse files
add 1252
1 parent8b2a4f5 commite6e57b0

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +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||
3031
|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||
3132
|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||
3233
|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||
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
packagecom.fishercoder.solutions;
2+
3+
/**
4+
* 1252. Cells with Odd Values in a Matrix
5+
*
6+
* Given n and m which are the dimensions of a matrix initialized by zeros and given an array indices where indices[i] = [ri, ci]. For each pair of [ri, ci] you have to increment all cells in row ri and column ci by 1.
7+
* Return the number of cells with odd values in the matrix after applying the increment to all indices.
8+
*
9+
* Example 1:
10+
* 0, 0, 0 1, 2, 1 1, 3, 1
11+
* 0, 0, 0 0, 1, 0 1, 3 ,1
12+
*
13+
* Input: n = 2, m = 3, indices = [[0,1],[1,1]]
14+
* Output: 6
15+
* Explanation: Initial matrix = [[0,0,0],[0,0,0]].
16+
* After applying first increment it becomes [[1,2,1],[0,1,0]].
17+
* The final matrix will be [[1,3,1],[1,3,1]] which contains 6 odd numbers.
18+
*
19+
* Example 2:
20+
* 0, 0 0, 1 2, 2
21+
* 0, 0 1, 2 2, 2
22+
*
23+
* Input: n = 2, m = 2, indices = [[1,1],[0,0]]
24+
* Output: 0
25+
* Explanation: Final matrix = [[2,2],[2,2]]. There is no odd number in the final matrix.
26+
*
27+
*
28+
* Constraints:
29+
* 1 <= n <= 50
30+
* 1 <= m <= 50
31+
* 1 <= indices.length <= 100
32+
* 0 <= indices[i][0] < n
33+
* 0 <= indices[i][1] < m
34+
* */
35+
publicclass_1252 {
36+
publicstaticclassSolution1 {
37+
publicintoddCells(intn,intm,int[][]indices) {
38+
int[][]matrix =newint[n][m];
39+
for (inti =0;i <indices.length;i++) {
40+
addOneToRow(matrix,indices[i][0]);
41+
addOneToColumn(matrix,indices[i][1]);
42+
}
43+
intoddNumberCount =0;
44+
for (inti =0;i <matrix.length;i++) {
45+
for (intj =0;j <matrix[0].length;j++) {
46+
if (matrix[i][j] %2 !=0) {
47+
oddNumberCount++;
48+
}
49+
}
50+
}
51+
returnoddNumberCount;
52+
}
53+
54+
privatevoidaddOneToColumn(int[][]matrix,intcolumnIndex) {
55+
for (inti =0;i <matrix.length;i++) {
56+
matrix[i][columnIndex] +=1;
57+
}
58+
}
59+
60+
privatevoidaddOneToRow(int[][]matrix,introwIndex) {
61+
for (intj =0;j <matrix[0].length;j++) {
62+
matrix[rowIndex][j] +=1;
63+
}
64+
}
65+
}
66+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1252;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1252Test {
10+
privatestatic_1252.Solution1solution1;
11+
privatestaticint[][]indices;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_1252.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
indices =newint[][]{
21+
{0,1},
22+
{1,1}
23+
};
24+
assertEquals(6,solution1.oddCells(2,3,indices));
25+
}
26+
27+
@Test
28+
publicvoidtest2() {
29+
indices =newint[][]{
30+
{1,1},
31+
{0,0}
32+
};
33+
assertEquals(0,solution1.oddCells(2,2,indices));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp