File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed
lcof2/剑指 Offer II 003. 前 n 个数字二进制中 1 的个数 Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -144,6 +144,23 @@ function countBits(n: number): number[] {
144
144
}
145
145
```
146
146
147
+ #### Swift
148
+
149
+ ``` swift
150
+ class Solution {
151
+ func countBits (_ n : Int ) -> [Int ] {
152
+ if n == 0 {
153
+ return [0 ]
154
+ }
155
+ var f = [Int ](repeating : 0 , count : n + 1 )
156
+ for i in 1 ... n {
157
+ f[i] = f[i & (i - 1 )] + 1
158
+ }
159
+ return f
160
+ }
161
+ }
162
+ ```
163
+
147
164
<!-- tabs: end -->
148
165
149
166
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func countBits( _ n: Int ) -> [ Int ] {
3
+ if n == 0 {
4
+ return [ 0 ]
5
+ }
6
+ var f = [ Int] ( repeating: 0 , count: n + 1 )
7
+ for i in 1 ... n {
8
+ f [ i] = f [ i & ( i - 1 ) ] + 1
9
+ }
10
+ return f
11
+ }
12
+ }
You can’t perform that action at this time.
0 commit comments