File tree 8 files changed +93
-28
lines changed
solution/0300-0399/0338.Counting Bits
8 files changed +93
-28
lines changed Original file line number Diff line number Diff line change 1
1
<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 >
3
3
</p >
4
4
5
5
<p align =" center " >
166
166
1 . 你也可以参考帮助文档 https://help.github.com/cn 了解更多细节。
167
167
168
168
<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 >
170
170
</p >
171
171
172
172
[ Gitpod.io] ( https://www.gitpod.io ) 是一个免费的在线开发环境,你也可以使用它参与本项目。
200
200
201
201
知名互联网科技博主 [ @爱可可-爱生活] ( https://weibo.com/fly51fly ) 微博推荐。
202
202
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 >
204
204
205
205
## 许可证
206
206
Original file line number Diff line number Diff line change 1
1
<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 >
3
3
</p >
4
4
5
5
<p align =" center " >
@@ -160,7 +160,7 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http
160
160
1 . See [ CONTRIBUTING] ( https://github.com/doocs/.github/blob/main/CONTRIBUTING.md ) or [ GitHub Help] ( https://help.github.com/en ) for more details.
161
161
162
162
<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 >
164
164
</p >
165
165
166
166
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
171
171
172
172
<a href =" https://github.com/doocs/leetcode/stargazers " target =" _blank " ><img src =" https://starchart.cc/doocs/leetcode.svg " alt =" Stargazers over time " /></a >
173
173
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> -->
175
175
176
176
## Contributors
177
177
Original file line number Diff line number Diff line change 60
60
61
61
<!-- 这里可写通用的实现逻辑 -->
62
62
63
+ ** 方法一:位运算**
64
+
63
65
<!-- tabs:start -->
64
66
65
67
### ** Python3**
66
68
67
69
<!-- 这里可写当前语言的特殊实现逻辑 -->
68
70
69
71
``` 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
71
78
```
72
79
73
80
### ** Java**
74
81
75
82
<!-- 这里可写当前语言的特殊实现逻辑 -->
76
83
77
84
``` 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
+ }
79
94
```
80
95
81
96
### ** C++**
84
99
class Solution {
85
100
public:
86
101
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;
93
105
}
94
106
};
95
107
```
96
108
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
+
97
121
### ** ...**
98
122
99
123
```
Original file line number Diff line number Diff line change 54
54
### ** Python3**
55
55
56
56
``` 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
58
63
```
59
64
60
65
### ** Java**
61
66
62
67
``` 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
+ }
64
77
```
65
78
66
79
### ** C++**
69
82
class Solution {
70
83
public:
71
84
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;
78
88
}
79
89
};
80
90
```
81
91
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
+
82
104
### ** ...**
83
105
84
106
```
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
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;
10
7
}
11
8
};
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments