Skip to content

Commit 4908873

Browse files
committed
feat: add solutions to lc problem: No.0338
No.0338.Counting Bits
1 parent df37c33 commit 4908873

File tree

8 files changed

+93
-28
lines changed

8 files changed

+93
-28
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p align="center">
2-
<a href="https://github.com/doocs/leetcode"><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/leetcode-doocs.png" alt="LeetCode-GitHub-Doocs"></a>
2+
<a href="https://github.com/doocs/leetcode"><img src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/gh/doocs/leetcode@main/images/leetcode-doocs.png" alt="LeetCode-GitHub-Doocs"></a>
33
</p>
44

55
<p align="center">
@@ -166,7 +166,7 @@
166166
1. 你也可以参考帮助文档 https://help.github.com/cn 了解更多细节。
167167

168168
<p align="center">
169-
  <a href="https://github.com/doocs/leetcode"><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/how-to-contribute.svg" alt="how-to-contribute"></a>
169+
  <a href="https://github.com/doocs/leetcode"><img src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/gh/doocs/leetcode@main/images/how-to-contribute.svg" alt="how-to-contribute"></a>
170170
</p>
171171

172172
[Gitpod.io](https://www.gitpod.io) 是一个免费的在线开发环境,你也可以使用它参与本项目。
@@ -200,7 +200,7 @@
200200

201201
知名互联网科技博主 [@爱可可-爱生活](https://weibo.com/fly51fly) 微博推荐。
202202

203-
<a href="https://weibo.com/fly51fly" target="_blank"><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/recommender-fly51fly.png"></a>
203+
<a href="https://weibo.com/fly51fly" target="_blank"><img src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/gh/doocs/leetcode@main/images/recommender-fly51fly.png"></a>
204204

205205
## 许可证
206206

README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p align="center">
2-
<a href="https://github.com/doocs/leetcode"><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/leetcode-doocs.png" alt="LeetCode-GitHub-Doocs"></a>
2+
<a href="https://github.com/doocs/leetcode"><img src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/gh/doocs/leetcode@main/images/leetcode-doocs.png" alt="LeetCode-GitHub-Doocs"></a>
33
</p>
44

55
<p align="center">
@@ -160,7 +160,7 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http
160160
1. See [CONTRIBUTING](https://github.com/doocs/.github/blob/main/CONTRIBUTING.md) or [GitHub Help](https://help.github.com/en) for more details.
161161

162162
<p align="center">
163-
  <a href="https://github.com/doocs/leetcode"><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/how-to-contribute.svg" alt="how-to-contribute"></a>
163+
  <a href="https://github.com/doocs/leetcode"><img src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/gh/doocs/leetcode@main/images/how-to-contribute.svg" alt="how-to-contribute"></a>
164164
</p>
165165

166166
You can also contribute to [doocs/leetcode](https://github.com/doocs/leetcode) using [Gitpod.io](https://www.gitpod.io), a free online dev environment with a single click.
@@ -171,7 +171,7 @@ You can also contribute to [doocs/leetcode](https://github.com/doocs/leetcode) u
171171

172172
<a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://starchart.cc/doocs/leetcode.svg" alt="Stargazers over time" /></a>
173173

174-
<!-- <a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/starcharts.svg" alt="Stargazers over time" /></a> -->
174+
<!-- <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> -->
175175

176176
## Contributors
177177

solution/0300-0399/0338.Counting Bits/README.md

+32-8
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,37 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:位运算**
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**
6668

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

6971
```python
70-
72+
class Solution:
73+
def countBits(self, n: int) -> List[int]:
74+
ans = [0] * (n + 1)
75+
for i in range(1, n + 1):
76+
ans[i] = ans[i & (i - 1)] + 1
77+
return ans
7178
```
7279

7380
### **Java**
7481

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

7784
```java
78-
85+
class Solution {
86+
public int[] countBits(int n) {
87+
int[] ans = new int[n + 1];
88+
for (int i = 1; i <= n; ++i) {
89+
ans[i] = ans[i & (i - 1)] + 1;
90+
}
91+
return ans;
92+
}
93+
}
7994
```
8095

8196
### **C++**
@@ -84,16 +99,25 @@
8499
class Solution {
85100
public:
86101
vector<int> countBits(int n) {
87-
vector<int> res(n + 1);
88-
for (int i = 1; i <= n; i++) {
89-
res[i] = res[i & (i - 1)] + 1;
90-
}
91-
92-
return res;
102+
vector<int> ans(n + 1);
103+
for (int i = 1; i <= n; ++i) ans[i] = ans[i & (i - 1)] + 1;
104+
return ans;
93105
}
94106
};
95107
```
96108
109+
### **Go**
110+
111+
```go
112+
func countBits(n int) []int {
113+
ans := make([]int, n+1)
114+
for i := 1; i <= n; i++ {
115+
ans[i] = ans[i&(i-1)] + 1
116+
}
117+
return ans
118+
}
119+
```
120+
97121
### **...**
98122

99123
```

solution/0300-0399/0338.Counting Bits/README_EN.md

+30-8
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,26 @@
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def countBits(self, n: int) -> List[int]:
59+
ans = [0] * (n + 1)
60+
for i in range(1, n + 1):
61+
ans[i] = ans[i & (i - 1)] + 1
62+
return ans
5863
```
5964

6065
### **Java**
6166

6267
```java
63-
68+
class Solution {
69+
public int[] countBits(int n) {
70+
int[] ans = new int[n + 1];
71+
for (int i = 1; i <= n; ++i) {
72+
ans[i] = ans[i & (i - 1)] + 1;
73+
}
74+
return ans;
75+
}
76+
}
6477
```
6578

6679
### **C++**
@@ -69,16 +82,25 @@
6982
class Solution {
7083
public:
7184
vector<int> countBits(int n) {
72-
vector<int> res(n + 1);
73-
for (int i = 1; i <= n; i++) {
74-
res[i] = res[i & (i - 1)] + 1;
75-
}
76-
77-
return res;
85+
vector<int> ans(n + 1);
86+
for (int i = 1; i <= n; ++i) ans[i] = ans[i & (i - 1)] + 1;
87+
return ans;
7888
}
7989
};
8090
```
8191
92+
### **Go**
93+
94+
```go
95+
func countBits(n int) []int {
96+
ans := make([]int, n+1)
97+
for i := 1; i <= n; i++ {
98+
ans[i] = ans[i&(i-1)] + 1
99+
}
100+
return ans
101+
}
102+
```
103+
82104
### **...**
83105

84106
```
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
class Solution {
22
public:
33
vector<int> countBits(int n) {
4-
vector<int> res(n + 1);
5-
for (int i = 1; i <= n; i++) {
6-
res[i] = res[i & (i - 1)] + 1;
7-
}
8-
9-
return res;
4+
vector<int> ans(n + 1);
5+
for (int i = 1; i <= n; ++i) ans[i] = ans[i & (i - 1)] + 1;
6+
return ans;
107
}
118
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func countBits(n int) []int {
2+
ans := make([]int, n+1)
3+
for i := 1; i <= n; i++ {
4+
ans[i] = ans[i&(i-1)] + 1
5+
}
6+
return ans
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int[] countBits(int n) {
3+
int[] ans = new int[n + 1];
4+
for (int i = 1; i <= n; ++i) {
5+
ans[i] = ans[i & (i - 1)] + 1;
6+
}
7+
return ans;
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def countBits(self, n: int) -> List[int]:
3+
ans = [0] * (n + 1)
4+
for i in range(1, n + 1):
5+
ans[i] = ans[i & (i - 1)] + 1
6+
return ans

0 commit comments

Comments
 (0)