Skip to content

Commit 10ceaba

Browse files
committed
feat: add solutions to lc problem: No.2166
No.2166.Design Bitset
1 parent cbeeef8 commit 10ceaba

File tree

9 files changed

+691
-6
lines changed

9 files changed

+691
-6
lines changed

.github/workflows/starcharts.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: update-starcharts
22

33
on:
4+
schedule:
5+
- cron: "0 0/12 * * *"
46
workflow_dispatch:
57

68
jobs:
@@ -14,4 +16,4 @@ jobs:
1416
github_token: ${{ secrets.ACTION_TOKEN }}
1517
svg_path: images/starcharts.svg
1618
commit_message: "chore: auto update starcharts"
17-
stars_change: "50"
19+
stars_change: ${{ secrets.STARS_CHANGE }}

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,12 @@
180180

181181
## Stars 趋势
182182

183-
<a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://starchart.cc/doocs/leetcode.svg" alt="Stargazers over time" /></a>
183+
<!-- 使用 https://starchart.cc/ 自动刷新 stars 数据,若有问题,可以使用 GitHub Action: starcharts.yml -->
184+
<!-- <a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://starchart.cc/doocs/leetcode.svg" alt="Stargazers over time" /></a> -->
184185

185-
<!-- 这里先使用 https://starchart.cc/ 自动刷新 stars 数据,之后若有问题,可以使用 GitHub Action: starcharts.yml -->
186-
<!-- <a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="./images/starcharts.svg" alt="Stargazers over time" /></a> -->
186+
<!-- [![Star History Chart](https://api.star-history.com/svg?repos=doocs/leetcode&type=Date)](https://star-history.com/#doocs/leetcode) -->
187+
188+
<a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="./images/starcharts.svg" alt="Stargazers over time" /></a>
187189

188190
## 贡献者
189191

README_EN.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,11 @@ You can also contribute to [doocs/leetcode](https://github.com/doocs/leetcode) u
174174

175175
## Stargazers over time
176176

177-
<a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://starchart.cc/doocs/leetcode.svg" alt="Stargazers over time" /></a>
177+
<!-- <a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://starchart.cc/doocs/leetcode.svg" alt="Stargazers over time" /></a> -->
178178

179-
<!-- <a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/gh/doocs/leetcode@main/images/starcharts.svg" alt="Stargazers over time" /></a> -->
179+
<!-- [![Star History Chart](https://api.star-history.com/svg?repos=doocs/leetcode&type=Date)](https://star-history.com/#doocs/leetcode) -->
180+
181+
<a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="./images/starcharts.svg" alt="Stargazers over time" /></a>
180182

181183
## Contributors
182184

solution/2100-2199/2166.Design Bitset/README.md

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,244 @@ bs.toString(); // 返回 "01010" ,即 bitset 的当前组成情况。
6969
<!-- 这里可写当前语言的特殊实现逻辑 -->
7070

7171
```python
72+
class Bitset:
7273

74+
def __init__(self, size: int):
75+
self.a = ['0'] * size
76+
self.b = ['1'] * size
77+
self.cnt = 0
78+
79+
def fix(self, idx: int) -> None:
80+
if self.a[idx] == '0':
81+
self.a[idx] = '1'
82+
self.cnt += 1
83+
self.b[idx] = '0'
84+
85+
def unfix(self, idx: int) -> None:
86+
if self.a[idx] == '1':
87+
self.a[idx] = '0'
88+
self.cnt -= 1
89+
self.b[idx] = '1'
90+
91+
def flip(self) -> None:
92+
self.a, self.b = self.b, self.a
93+
self.cnt = len(self.a) - self.cnt
94+
95+
def all(self) -> bool:
96+
return self.cnt == len(self.a)
97+
98+
def one(self) -> bool:
99+
return self.cnt > 0
100+
101+
def count(self) -> int:
102+
return self.cnt
103+
104+
def toString(self) -> str:
105+
return ''.join(self.a)
106+
107+
108+
# Your Bitset object will be instantiated and called as such:
109+
# obj = Bitset(size)
110+
# obj.fix(idx)
111+
# obj.unfix(idx)
112+
# obj.flip()
113+
# param_4 = obj.all()
114+
# param_5 = obj.one()
115+
# param_6 = obj.count()
116+
# param_7 = obj.toString()
73117
```
74118

75119
### **Java**
76120

77121
<!-- 这里可写当前语言的特殊实现逻辑 -->
78122

79123
```java
124+
class Bitset {
125+
private char[] a;
126+
private char[] b;
127+
private int cnt;
128+
129+
public Bitset(int size) {
130+
a = new char[size];
131+
b = new char[size];
132+
Arrays.fill(a, '0');
133+
Arrays.fill(b, '1');
134+
}
135+
136+
public void fix(int idx) {
137+
if (a[idx] == '0') {
138+
a[idx] = '1';
139+
++cnt;
140+
}
141+
b[idx] = '0';
142+
}
143+
144+
public void unfix(int idx) {
145+
if (a[idx] == '1') {
146+
a[idx] = '0';
147+
--cnt;
148+
}
149+
b[idx] = '1';
150+
}
151+
152+
public void flip() {
153+
char[] t = a;
154+
a = b;
155+
b = t;
156+
cnt = a.length - cnt;
157+
}
158+
159+
public boolean all() {
160+
return cnt == a.length;
161+
}
162+
163+
public boolean one() {
164+
return cnt > 0;
165+
}
166+
167+
public int count() {
168+
return cnt;
169+
}
170+
171+
public String toString() {
172+
return String.valueOf(a);
173+
}
174+
}
175+
176+
/**
177+
* Your Bitset object will be instantiated and called as such:
178+
* Bitset obj = new Bitset(size);
179+
* obj.fix(idx);
180+
* obj.unfix(idx);
181+
* obj.flip();
182+
* boolean param_4 = obj.all();
183+
* boolean param_5 = obj.one();
184+
* int param_6 = obj.count();
185+
* String param_7 = obj.toString();
186+
*/
187+
```
188+
189+
### **C++**
190+
191+
```cpp
192+
class Bitset {
193+
public:
194+
string a, b;
195+
int cnt = 0;
196+
197+
Bitset(int size) {
198+
a = string(size, '0');
199+
b = string(size, '1');
200+
}
201+
202+
void fix(int idx) {
203+
if (a[idx] == '0') a[idx] = '1', ++cnt;
204+
b[idx] = '0';
205+
}
206+
207+
void unfix(int idx) {
208+
if (a[idx] == '1') a[idx] = '0', --cnt;
209+
b[idx] = '1';
210+
}
211+
212+
void flip() {
213+
swap(a, b);
214+
cnt = a.size() - cnt;
215+
}
216+
217+
bool all() {
218+
return cnt == a.size();
219+
}
220+
221+
bool one() {
222+
return cnt > 0;
223+
}
224+
225+
int count() {
226+
return cnt;
227+
}
228+
229+
string toString() {
230+
return a;
231+
}
232+
};
233+
234+
/**
235+
* Your Bitset object will be instantiated and called as such:
236+
* Bitset* obj = new Bitset(size);
237+
* obj->fix(idx);
238+
* obj->unfix(idx);
239+
* obj->flip();
240+
* bool param_4 = obj->all();
241+
* bool param_5 = obj->one();
242+
* int param_6 = obj->count();
243+
* string param_7 = obj->toString();
244+
*/
245+
```
246+
247+
### **Go**
248+
249+
```go
250+
type Bitset struct {
251+
a []byte
252+
b []byte
253+
cnt int
254+
}
255+
256+
func Constructor(size int) Bitset {
257+
a := bytes.Repeat([]byte{'0'}, size)
258+
b := bytes.Repeat([]byte{'1'}, size)
259+
return Bitset{a, b, 0}
260+
}
261+
262+
func (this *Bitset) Fix(idx int) {
263+
if this.a[idx] == '0' {
264+
this.a[idx] = '1'
265+
this.cnt++
266+
}
267+
this.b[idx] = '0'
268+
}
269+
270+
func (this *Bitset) Unfix(idx int) {
271+
if this.a[idx] == '1' {
272+
this.a[idx] = '0'
273+
this.cnt--
274+
}
275+
this.b[idx] = '1'
276+
}
277+
278+
func (this *Bitset) Flip() {
279+
this.a, this.b = this.b, this.a
280+
this.cnt = len(this.a) - this.cnt
281+
}
282+
283+
func (this *Bitset) All() bool {
284+
return this.cnt == len(this.a)
285+
}
286+
287+
func (this *Bitset) One() bool {
288+
return this.cnt > 0
289+
}
290+
291+
func (this *Bitset) Count() int {
292+
return this.cnt
293+
}
294+
295+
func (this *Bitset) ToString() string {
296+
return string(this.a)
297+
}
80298
299+
/**
300+
* Your Bitset object will be instantiated and called as such:
301+
* obj := Constructor(size);
302+
* obj.Fix(idx);
303+
* obj.Unfix(idx);
304+
* obj.Flip();
305+
* param_4 := obj.All();
306+
* param_5 := obj.One();
307+
* param_6 := obj.Count();
308+
* param_7 := obj.ToString();
309+
*/
81310
```
82311

83312
### **TypeScript**

0 commit comments

Comments
 (0)