1. 程式人生 > >[Swift]LeetCode338. 位元位計數 | Counting Bits

[Swift]LeetCode338. 位元位計數 | Counting Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.

Example 1:

Input: 2
Output: [0,1,1]

Example 2:

Input: 5
Output: [0,1,1,2,1,2]

Follow up:

  • It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n)/possibly in a single pass?
  • Space complexity should be O(n).
  • Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.

給定一個非負整數 num。對於 0 ≤ i ≤ num 範圍中的每個數字 i ,計算其二進位制數中的 1 的數目並將它們作為陣列返回。

示例 1:

輸入: 2
輸出: [0,1,1]

示例 2:

輸入: 5
輸出: [0,1,1,2,1,2]

進階:

  • 給出時間複雜度為O(n*sizeof(integer))的解答非常容易。但你可以線上性時間O(n)內用一趟掃描做到嗎?
  • 要求演算法的空間複雜度為O(n)。
  • 你能進一步完善解法嗎?要求在C++或任何其他語言中不使用任何內建函式(如 C++ 中的 __builtin_popcount)來執行此操作。

36ms

 1 class Solution {
 2     func countBits(_ num: Int) -> [Int] {
 3         guard num > 0
else { 4 return [0] 5 } 6 7 var result = [0] 8 var i = 0 9 var total = 1 10 11 for j in 1...num { 12 result.append(result[i] + 1) 13 i += 1 14 if i == total { 15 i = 0 16 total = j + 1 17 } 18 } 19 return result 20 } 21 }

40ms

 1 class Solution {
 2     func countBits(_ num: Int) -> [Int] {
 3         if num == 0 {
 4             return [0]
 5         }
 6         var results = [Int]()
 7         results.append(0)
 8         for n in 1...num {
 9             if n%2 == 1 {
10                 results.append(results[n-1]+1)
11             } else {
12                 results.append(results[n/2])
13             }
14         }
15         return results
16     }
17 }

44ms

 1 class Solution {
 2     func countBits(_ num: Int) -> [Int] {
 3         guard num > 0 else {
 4             return [0]
 5         }
 6 
 7         var result = Array(repeating: 0, count: num + 1)
 8         result[1] = 1
 9         var loopCount = 2
10         var index = 2
11         while index <= num {
12             for j in 0..<loopCount {
13                 if index > num {
14                     break
15                 }
16 
17                 result[index] = 1 + result[j]
18                 index += 1
19             }
20 
21             loopCount *= 2
22         }
23         return result
24     }
25 }

48ms

 1 class Solution {
 2     func countBits(_ num: Int) -> [Int] {
 3         var a = [0]
 4         var b = [Int]()
 5         while (a.count + b.count) != num+1 {
 6             if a.count == b.count{
 7                 a = a + b
 8                 b = [Int]()
 9             }
10             b.append(a[b.count]+1)
11         }
12         return a + b
13     }
14 }

88ms

 1 class Solution {
 2     func countBits(_ num: Int) -> [Int] {
 3         guard num > 0 else { return [0] }
 4         var dp = [Int](repeating: 0, count: num + 1)
 5         
 6         for i in 1...num {
 7             dp[i] = dp[i & (i - 1)] + 1
 8         }
 9         return dp
10     }
11 }

108ms

 1 class Solution {
 2     func countBits(_ num: Int) -> [Int] {
 3         var result = [Int]()
 4         if num < 0 {
 5             return result
 6         }
 7         result.append(0)
 8         if num == 0 {
 9             return result
10         }
11         for i in 1 ... num {
12             print(i & 1)
13             result.append(result[i >> 1] + (i & 1))
14         }
15         return result
16     }
17 }

112ms

 1 class Solution {
 2     func countBits(_ num: Int) -> [Int] {       
 3         if num == 0 { return [0] }
 4         var result = [0]
 5         var count = 1
 6         while true {
 7             for j in 0 ..< count {
 8                 result.append(result[j] + 1)    
 9                 if result.count == num + 1 {
10                     return result
11                 }
12             }
13             count = result.count
14         }
15         return result
16         
17     }
18 }

116ms

 1 class Solution {
 2     func countBits(_ num: Int) -> [Int] {
 3         if num == 0 {
 4             return [0]
 5         }
 6         var res = [Int].init()
 7         res.append(0)
 8         for n in 1...num {
 9             let count = res[n & (n - 1)] + 1
10             res.append(count)
11         }
12         return res
13     }
14 }

164ms

 1 class Solution {
 2     func countBits(_ num: Int) -> [Int] {
 3         var b = [Int](repeating: 0, count: num + 1)
 4         if num == 0 { return [0] }
 5         if num == 1 { return [0,1] }
 6         for i in 1...num {
 7             b[i] = b[i >> 1] + i % 2
 8         }
 9         return b
10     }
11 }

176ms

 1 class Solution {
 2     func countBits(_ num: Int) -> [Int]
 3     {
 4         var res = [Int]()
 5         for i in 0...num
 6         {
 7             var n = i
 8             var count = 0
 9             while n > 0
10             {
11                 if n&1 == 1
12                 {
13                     count += 1
14                 }
15                 n = n >> 1
16             }
17             res.append(count)
18         }
19         return res
20     }
21 }

156ms

 1 class Solution {
 2     func countBits(_ num: Int) -> [Int] {
 3         guard num > 0 else { return [0] }
 4         var dp = [Int](repeating: 0, count: num + 1)
 5         
 6         for i in 1...num {
 7             dp[i] = dp[i / 2]
 8             if i % 2 == 1 {
 9                 dp[i] += 1
10             }
11         }
12         return dp
13     }
14 }