# Code language: Python classSolution: defoddCells(self, m: int, n: int, indices: List[List[int]]) -> int: mat = [[0for _ inrange(n)] for _ inrange(m)] for r, c in indices: for i inrange(n): mat[r][i] += 1 for i inrange(m): mat[i][c] += 1 returnsum(c % 2 == 1for r in mat for c in r)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Code language: Java classSolution { publicintoddCells(int m, int n, int[][] indices) { int[][] mat = newint[m][n]; for (inti=0, size = indices.length; i < size; ++i) { intr= indices[i][0], c = indices[i][1]; for (intj=0; j < m; ++j) mat[j][c]++; for (intj=0; j < n; ++j) mat[r][j]++; } intcnt=0; for (inti=0; i < m; ++i) { for (intj=0; j < n; ++j) { if (mat[i][j] % 2 == 1) ++cnt; } } return cnt; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Code language: C++ classSolution { public: intoddCells(int m, int n, vector<vector<int>>& indices){ vector<vector<int>> mat(m, vector<int>(n)); for (int i = 0, size = indices.size(); i < size; ++i) { int r = indices[i][0], c = indices[i][1]; for (int j = 0; j < m; ++j) mat[j][c]++; for (int j = 0; j < n; ++j) mat[r][j]++; } int cnt = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (mat[i][j] % 2 == 1) ++cnt; } } return cnt; } };
/** Code language: JavaScript * @param {number} m * @param {number} n * @param {number[][]} indices * @return {number} */ var oddCells = function(m, n, indices) { var mat = newArray(m).fill(0).map(() =>newArray(n).fill(0)); for (let i = 0, size = indices.length; i < size; ++i) { let r = indices[i][0], c = indices[i][1]; for (let j = 0; j < m; ++j) mat[j][c]++; for (let j = 0; j < n; ++j) mat[r][j]++; } let cnt = 0; for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { if (mat[i][j] % 2 == 1) ++cnt; } } return cnt; };
时间复杂度: \(O(k \times (n + m) +
nm)\), \(n, m\)
表示矩阵的列数和行数, \(k\) 表示
indices 的长度
for r, c in indices: row[r] += 1 col[c] += 1 a = sum(r % 2 == 1for r in row) b = len(row) - a c = sum(c % 2 == 1for c in col) d = len(col) - c return a * d + b * c
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Code language: Java classSolution { publicintoddCells(int m, int n, int[][] indices) { int[] row = newint[m], col = newint[n]; for (inti=0, size = indices.length; i < size; ++i) { intr= indices[i][0], c = indices[i][1]; row[r]++; col[c]++; } inta=0, b = 0; for (inti=0; i < m; ++i) if (row[i] % 2 == 1) ++a; for (inti=0; i < n; ++i) if (col[i] % 2 == 1) ++b; return a * (n - b) + (m - a) * b; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Code language: C++ classSolution { public: intoddCells(int m, int n, vector<vector<int>>& indices){ vector<int> row(m), col(n); for (int i = 0, size = indices.size(); i < size; ++i) { int r = indices[i][0], c = indices[i][1]; row[r]++; col[c]++; } int a = 0, b = 0; for (int i = 0; i < m; ++i) if (row[i] % 2 == 1) ++a; for (int i = 0; i < n; ++i) if (col[i] % 2 == 1) ++b; return a * (n - b) + (m - a) * b; } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** Code language: JavaScript * @param {number} m * @param {number} n * @param {number[][]} indices * @return {number} */ var oddCells = function(m, n, indices) { const row = Array(m).fill(0), col = Array(n).fill(0); for (let i = 0, size = indices.length; i < size; ++i) { let r = indices[i][0], c = indices[i][1]; row[r]++; col[c]++; } let a = 0, b = 0; for (let i = 0; i < m; ++i) if (row[i] % 2 == 1) ++a; for (let i = 0; i < n; ++i) if (col[i] % 2 == 1) ++b; return a * (n - b) + (m - a) * b; };
时间复杂度: \(O(k + n + m)\), \(n, m\) 表示矩阵的列数和行数, \(k\) 表示 indices 的长度