forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_2166.java
88 lines (78 loc) · 2.39 KB
/
_2166.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.fishercoder.solutions;
public class _2166 {
public static class Solution1 {
class Bitset {
int size;
int ones;
int[] nums1;
int[] nums2;
boolean checkingNums1;
public Bitset(int size) {
this.nums1 = new int[size];
this.nums2 = new int[size];
for (int i = 0; i < size; i++) {
nums2[i] = 1;
}
this.ones = 0;
this.size = size;
this.checkingNums1 = true;
}
public void fix(int idx) {
if (this.checkingNums1) {
if (nums1[idx] == 0) {
ones++;
}
nums1[idx] = 1;
nums2[idx] = 0;
} else {
if (nums2[idx] == 0) {
ones++;
}
nums2[idx] = 1;
nums1[idx] = 0;
}
}
public void unfix(int idx) {
if (this.checkingNums1) {
if (nums1[idx] == 1) {
ones--;
}
nums1[idx] = 0;
nums2[idx] = 1;
} else {
if (nums2[idx] == 1) {
ones--;
}
nums2[idx] = 0;
nums1[idx] = 1;
}
}
public void flip() {
this.ones = this.size - this.ones;
this.checkingNums1 = !this.checkingNums1;
}
public boolean all() {
return ones == size;
}
public boolean one() {
return ones > 0;
}
public int count() {
return ones;
}
public String toString() {
StringBuilder sb = new StringBuilder();
if (this.checkingNums1) {
for (int i = 0; i < size; i++) {
sb.append(nums1[i]);
}
} else {
for (int i = 0; i < size; i++) {
sb.append(nums2[i]);
}
}
return sb.toString();
}
}
}
}