Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cd7a6ad

Browse files
add 2166
1 parent 4681e47 commit cd7a6ad

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|2166|[Design Bitset](https://leetcode.com/problems/design-bitset/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2166.java) ||Medium||
1112
|2165|[Smallest Value of the Rearranged Number](https://leetcode.com/problems/smallest-value-of-the-rearranged-number/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2165.java) ||Medium||
1213
|2164|[Sort Even and Odd Indices Independently](https://leetcode.com/problems/sort-even-and-odd-indices-independently/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2164.java) ||Easy||
1314
|2161|[Partition Array According to Given Pivot](https://leetcode.com/problems/partition-array-according-to-given-pivot/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2161.java) ||Medium||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2166 {
4+
public static class Solution1 {
5+
class Bitset {
6+
7+
int size;
8+
int ones;
9+
int[] nums1;
10+
int[] nums2;
11+
boolean checkingNums1;
12+
13+
public Bitset(int size) {
14+
this.nums1 = new int[size];
15+
this.nums2 = new int[size];
16+
for (int i = 0; i < size; i++) {
17+
nums2[i] = 1;
18+
}
19+
this.ones = 0;
20+
this.size = size;
21+
this.checkingNums1 = true;
22+
}
23+
24+
public void fix(int idx) {
25+
if (this.checkingNums1) {
26+
if (nums1[idx] == 0) {
27+
ones++;
28+
}
29+
nums1[idx] = 1;
30+
nums2[idx] = 0;
31+
} else {
32+
if (nums2[idx] == 0) {
33+
ones++;
34+
}
35+
nums2[idx] = 1;
36+
nums1[idx] = 0;
37+
}
38+
}
39+
40+
public void unfix(int idx) {
41+
if (this.checkingNums1) {
42+
if (nums1[idx] == 1) {
43+
ones--;
44+
}
45+
nums1[idx] = 0;
46+
nums2[idx] = 1;
47+
} else {
48+
if (nums2[idx] == 1) {
49+
ones--;
50+
}
51+
nums2[idx] = 0;
52+
nums1[idx] = 1;
53+
}
54+
}
55+
56+
public void flip() {
57+
this.ones = this.size - this.ones;
58+
this.checkingNums1 = !this.checkingNums1;
59+
}
60+
61+
public boolean all() {
62+
return ones == size;
63+
}
64+
65+
public boolean one() {
66+
return ones > 0;
67+
}
68+
69+
public int count() {
70+
return ones;
71+
}
72+
73+
public String toString() {
74+
StringBuilder sb = new StringBuilder();
75+
if (this.checkingNums1) {
76+
for (int i = 0; i < size; i++) {
77+
sb.append(nums1[i]);
78+
}
79+
} else {
80+
for (int i = 0; i < size; i++) {
81+
sb.append(nums2[i]);
82+
}
83+
}
84+
return sb.toString();
85+
}
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)