Skip to content

Commit ff06d9e

Browse files
authored
feat: add swift implementation to lcof2 problem: No.003 (doocs#2967)
1 parent 3a7a59e commit ff06d9e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lcof2/剑指 Offer II 003. 前 n 个数字二进制中 1 的个数/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,23 @@ function countBits(n: number): number[] {
144144
}
145145
```
146146

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+
147164
<!-- tabs:end -->
148165

149166
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

0 commit comments

Comments
 (0)