-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNumberOfOddaluedCells.java
More file actions
37 lines (31 loc) · 1.12 KB
/
NumberOfOddaluedCells.java
File metadata and controls
37 lines (31 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package swordPointOffer;
/**
* @Author: Wenhang Chen
* @Description:
* @Date: Created in 21:04 5/2/2020
* @Modified by:
*/
public class NumberOfOddaluedCells {
// 奇 偶的加减可以转变成boolean的true 和 false
// 行列的加减可以统一用两个数组解决
// 最后根据公式(规律)来得出奇偶的数量
// 最后得到的是rr * m + cc * n - rr * cc * 2,原本行列奇数的数量相加为rr * m + cc * n - rr * cc,但是当行列同时为奇数时,重叠部分就成了偶数了。
//
// 所以 最后的结果是rr * m + cc * n - rr * cc * 2
public int oddCells(int n, int m, int[][] indices) {
boolean[] r = new boolean[n];
boolean[] c = new boolean[m];
for (int i = 0; i < indices.length; i++) {
r[indices[i][0]] = !r[indices[i][0]];
c[indices[i][1]] = !c[indices[i][1]];
}
int rr = 0, cc = 0;
for (int i = 0; i < r.length; i++) {
if (r[i]) rr++;
}
for (int i = 0; i < c.length; i++) {
if (c[i]) cc++;
}
return rr * m + cc * n - rr * cc * 2;
}
}