We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5063d27 commit 183f9b1Copy full SHA for 183f9b1
counting-bits/WhiteHyun.swift
@@ -0,0 +1,27 @@
1
+//
2
+// 338. Counting Bits
3
+// https://leetcode.com/problems/counting-bits/description/
4
+// Dale-Study
5
6
+// Created by WhiteHyun on 2024/05/19.
7
8
+
9
+final class Solution {
10
11
+ // MARK: - Time Complexity: O(n), Space Complexity: O(n)
12
13
+ func countBits(_ n: Int) -> [Int] {
14
+ var array: [Int] = .init(repeating: 0, count: n + 1)
15
+ for i in stride(from: 1, through: n, by: 1) {
16
+ array[i] = array[i >> 1] + (i & 1)
17
+ }
18
+ return array
19
20
21
+ // MARK: - nonzeroBitCount
22
23
+ func countBits2(_ n: Int) -> [Int] {
24
+ return (0...n).map(\.nonzeroBitCount)
25
26
27
+}
0 commit comments