题目(难度:简单):
代码思想:
单纯的暴力解决,需要注意的是区分indices的对行还是对列操作,这里是对行队列交替执行加1操作,用一个boolean类型的flag区分一下。
代码实现:
public int oddCells(int n, int m, int[][] indices) {int[][] array = new int[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {array[i][j] = 0;}}int row = indices.length - 1;int col = indices[0].length - 1;boolean flag = true;for (int i = 0; i <= row; i++) {for (int j = 0; j <= col; j++) {/*对行增加*/int val = indices[i][j];if (flag == true) {for (int l = 0; l < m; l++) {array[val][l] += 1;}flag = false;} else { //对列操作for (int l = 0; l < n; l++) {array[l][val] += 1;}flag = true;}}}int count = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (array[i][j] % 2 != 0) {count++;}}}return count;}
测试用例:
算法分析:
时间复杂度,额外空间复杂度O(m*n)