Skip to content

Commit 3ae1177

Browse files
committed
feat: add solutions to lc problem: No.1252
No.1252.Cells with Odd Values in a Matrix
1 parent 528cb9f commit 3ae1177

File tree

6 files changed

+225
-2
lines changed

6 files changed

+225
-2
lines changed

solution/1200-1299/1252.Cells with Odd Values in a Matrix/README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,92 @@
6969
<!-- 这里可写当前语言的特殊实现逻辑 -->
7070

7171
```python
72-
72+
class Solution:
73+
def oddCells(self, m: int, n: int, indices: List[List[int]]) -> int:
74+
g = [[0] * n for _ in range(m)]
75+
for r, c in indices:
76+
for i in range(m):
77+
g[i][c] += 1
78+
for j in range(n):
79+
g[r][j] += 1
80+
return sum(g[i][j] % 2 for i in range(m) for j in range(n))
7381
```
7482

7583
### **Java**
7684

7785
<!-- 这里可写当前语言的特殊实现逻辑 -->
7886

7987
```java
88+
class Solution {
89+
public int oddCells(int m, int n, int[][] indices) {
90+
int[][] g = new int[m][n];
91+
for (int[] e : indices) {
92+
int r = e[0], c = e[1];
93+
for (int i = 0; i < m; ++i) {
94+
++g[i][c];
95+
}
96+
for (int j = 0; j < n; ++j) {
97+
++g[r][j];
98+
}
99+
}
100+
int ans = 0;
101+
for (int i = 0; i < m; ++i) {
102+
for (int j = 0; j < n; ++j) {
103+
ans += g[i][j] % 2;
104+
}
105+
}
106+
return ans;
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
int oddCells(int m, int n, vector<vector<int>>& indices) {
117+
vector<vector<int>> g(m, vector<int>(n));
118+
for (auto& e : indices)
119+
{
120+
int r = e[0], c = e[1];
121+
for (int i = 0; i < m; ++i) ++g[i][c];
122+
for (int j = 0; j < n; ++j) ++g[r][j];
123+
}
124+
int ans = 0;
125+
for (int i = 0; i < m; ++i)
126+
for (int j = 0; j < n; ++j)
127+
ans += g[i][j] % 2;
128+
return ans;
129+
}
130+
};
131+
```
80132
133+
### **Go**
134+
135+
```go
136+
func oddCells(m int, n int, indices [][]int) int {
137+
g := make([][]int, m)
138+
for i := range g {
139+
g[i] = make([]int, n)
140+
}
141+
for _, e := range indices {
142+
r, c := e[0], e[1]
143+
for i := 0; i < m; i++ {
144+
g[i][c]++
145+
}
146+
for j := 0; j < n; j++ {
147+
g[r][j]++
148+
}
149+
}
150+
ans := 0
151+
for i := 0; i < m; i++ {
152+
for j := 0; j < n; j++ {
153+
ans += g[i][j] % 2
154+
}
155+
}
156+
return ans
157+
}
81158
```
82159

83160
### **...**

solution/1200-1299/1252.Cells with Odd Values in a Matrix/README_EN.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,90 @@ The final matrix is [[1,3,1],[1,3,1]], which contains 6 odd numbers.
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def oddCells(self, m: int, n: int, indices: List[List[int]]) -> int:
59+
g = [[0] * n for _ in range(m)]
60+
for r, c in indices:
61+
for i in range(m):
62+
g[i][c] += 1
63+
for j in range(n):
64+
g[r][j] += 1
65+
return sum(g[i][j] % 2 for i in range(m) for j in range(n))
5866
```
5967

6068
### **Java**
6169

6270
```java
71+
class Solution {
72+
public int oddCells(int m, int n, int[][] indices) {
73+
int[][] g = new int[m][n];
74+
for (int[] e : indices) {
75+
int r = e[0], c = e[1];
76+
for (int i = 0; i < m; ++i) {
77+
++g[i][c];
78+
}
79+
for (int j = 0; j < n; ++j) {
80+
++g[r][j];
81+
}
82+
}
83+
int ans = 0;
84+
for (int i = 0; i < m; ++i) {
85+
for (int j = 0; j < n; ++j) {
86+
ans += g[i][j] % 2;
87+
}
88+
}
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int oddCells(int m, int n, vector<vector<int>>& indices) {
100+
vector<vector<int>> g(m, vector<int>(n));
101+
for (auto& e : indices)
102+
{
103+
int r = e[0], c = e[1];
104+
for (int i = 0; i < m; ++i) ++g[i][c];
105+
for (int j = 0; j < n; ++j) ++g[r][j];
106+
}
107+
int ans = 0;
108+
for (int i = 0; i < m; ++i)
109+
for (int j = 0; j < n; ++j)
110+
ans += g[i][j] % 2;
111+
return ans;
112+
}
113+
};
114+
```
63115
116+
### **Go**
117+
118+
```go
119+
func oddCells(m int, n int, indices [][]int) int {
120+
g := make([][]int, m)
121+
for i := range g {
122+
g[i] = make([]int, n)
123+
}
124+
for _, e := range indices {
125+
r, c := e[0], e[1]
126+
for i := 0; i < m; i++ {
127+
g[i][c]++
128+
}
129+
for j := 0; j < n; j++ {
130+
g[r][j]++
131+
}
132+
}
133+
ans := 0
134+
for i := 0; i < m; i++ {
135+
for j := 0; j < n; j++ {
136+
ans += g[i][j] % 2
137+
}
138+
}
139+
return ans
140+
}
64141
```
65142

66143
### **...**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int oddCells(int m, int n, vector<vector<int>>& indices) {
4+
vector<vector<int>> g(m, vector<int>(n));
5+
for (auto& e : indices)
6+
{
7+
int r = e[0], c = e[1];
8+
for (int i = 0; i < m; ++i) ++g[i][c];
9+
for (int j = 0; j < n; ++j) ++g[r][j];
10+
}
11+
int ans = 0;
12+
for (int i = 0; i < m; ++i)
13+
for (int j = 0; j < n; ++j)
14+
ans += g[i][j] % 2;
15+
return ans;
16+
}
17+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func oddCells(m int, n int, indices [][]int) int {
2+
g := make([][]int, m)
3+
for i := range g {
4+
g[i] = make([]int, n)
5+
}
6+
for _, e := range indices {
7+
r, c := e[0], e[1]
8+
for i := 0; i < m; i++ {
9+
g[i][c]++
10+
}
11+
for j := 0; j < n; j++ {
12+
g[r][j]++
13+
}
14+
}
15+
ans := 0
16+
for i := 0; i < m; i++ {
17+
for j := 0; j < n; j++ {
18+
ans += g[i][j] % 2
19+
}
20+
}
21+
return ans
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int oddCells(int m, int n, int[][] indices) {
3+
int[][] g = new int[m][n];
4+
for (int[] e : indices) {
5+
int r = e[0], c = e[1];
6+
for (int i = 0; i < m; ++i) {
7+
++g[i][c];
8+
}
9+
for (int j = 0; j < n; ++j) {
10+
++g[r][j];
11+
}
12+
}
13+
int ans = 0;
14+
for (int i = 0; i < m; ++i) {
15+
for (int j = 0; j < n; ++j) {
16+
ans += g[i][j] % 2;
17+
}
18+
}
19+
return ans;
20+
}
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def oddCells(self, m: int, n: int, indices: List[List[int]]) -> int:
3+
g = [[0] * n for _ in range(m)]
4+
for r, c in indices:
5+
for i in range(m):
6+
g[i][c] += 1
7+
for j in range(n):
8+
g[r][j] += 1
9+
return sum(g[i][j] % 2 for i in range(m) for j in range(n))

0 commit comments

Comments
 (0)