diff --git a/leetcode/338. Counting Bits/README.md b/leetcode/338. Counting Bits/README.md index 41c3a7c3..fddd43e8 100644 --- a/leetcode/338. Counting Bits/README.md +++ b/leetcode/338. Counting Bits/README.md @@ -1,26 +1,50 @@ -# [338. Counting Bits (Medium)](https://leetcode.com/problems/counting-bits/) +# [338. Counting Bits (Easy)](https://leetcode.com/problems/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.

+

Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.

+

 

Example 1:

-
Input: 2
-Output: [0,1,1]
+
Input: n = 2
+Output: [0,1,1]
+Explanation:
+0 --> 0
+1 --> 1
+2 --> 10
+

Example 2:

-
Input: 5
-Output: [0,1,1,2,1,2]
+
Input: n = 5
+Output: [0,1,1,2,1,2]
+Explanation:
+0 --> 0
+1 --> 1
+2 --> 10
+3 --> 11
+4 --> 100
+5 --> 101
 
-

Follow up:

+

 

+

Constraints:

+ + + +

 

+

Follow up:

+ +**Companies**: +[Amazon](https://leetcode.com/company/amazon), [Google](https://leetcode.com/company/google), [Apple](https://leetcode.com/company/apple) + **Related Topics**: [Dynamic Programming](https://leetcode.com/tag/dynamic-programming/), [Bit Manipulation](https://leetcode.com/tag/bit-manipulation/) @@ -42,14 +66,14 @@ And `1, 2, 3, ..., 2^k - 1` doesn't have bit overlap with `2^k`. // Space: O(1) class Solution { public: - vector countBits(int num) { - vector ans(num + 1); - for (int i = 1; i <= num; i *= 2) { - ans[i] = 1; - for (int j = 1; j < i && i + j <= num; ++j) ans[i + j] = ans[i] + ans[j]; + vector countBits(int n) { + vector ans(n + 1); + for (int i = 1; i <= n; i *= 2) { + ans[i] = 1; + for (int j = 1; j < i && i + j <= n; ++j) ans[i + j] = ans[i] + ans[j]; + } + return ans; } - return ans; - } }; ``` @@ -68,9 +92,9 @@ dp[i] = 1 + dp[i - (i & -i)] // Space: O(1) class Solution { public: - vector countBits(int num) { - vector ans(num + 1); - for (int i = 1; i <= num; ++i) ans[i] = 1 + ans[i - (i & -i)]; + vector countBits(int n) { + vector ans(n + 1); + for (int i = 1; i <= n; ++i) ans[i] = 1 + ans[i - (i & -i)]; return ans; } }; @@ -85,10 +109,32 @@ public: // Space: O(1) class Solution { public: - vector countBits(int num) { - vector ans(num + 1, 0); - for (int i = 1; i <= num; ++i) ans[i] = ans[i / 2] + (i % 2); - return ans; - } + vector countBits(int n) { + vector ans(n + 1); + for (int i = 1; i <= n; ++i) ans[i] = ans[i / 2] + (i % 2); + return ans; + } +}; +``` + +## Solution 4. DP + +DP based on highest bit + +```cpp +// OJ: https://leetcode.com/problems/counting-bits/ +// Author: github.com/lzl124631x +// Time: O(N) +// Space: O(1) +class Solution { +public: + vector countBits(int n) { + vector ans(n + 1); + for (int i = 1, hb = 1; i <= n; ++i) { + if ((hb & i) == 0) hb <<= 1; + ans[i] = 1 + ans[i - hb]; + } + return ans; + } }; ``` \ No newline at end of file diff --git a/leetcode/338. Counting Bits/s1.cpp b/leetcode/338. Counting Bits/s1.cpp deleted file mode 100644 index 711a1142..00000000 --- a/leetcode/338. Counting Bits/s1.cpp +++ /dev/null @@ -1,15 +0,0 @@ -// OJ: https://leetcode.com/problems/counting-bits -// Author: github.com/lzl124631x -// Time: O(N) -// Space: O(1) -class Solution { -public: - vector countBits(int num) { - vector ans(num + 1); - for (int i = 1; i <= num; i *= 2) { - ans[i] = 1; - for (int j = 1; j < i && i + j <= num; ++j) ans[i + j] = ans[i] + ans[j]; - } - return ans; - } -}; \ No newline at end of file diff --git a/leetcode/338. Counting Bits/s2.cpp b/leetcode/338. Counting Bits/s2.cpp deleted file mode 100644 index 7ca479bb..00000000 --- a/leetcode/338. Counting Bits/s2.cpp +++ /dev/null @@ -1,12 +0,0 @@ -// OJ: https://leetcode.com/problems/counting-bits/ -// Author: github.com/lzl124631x -// Time: O(N) -// Space: O(1) -class Solution { -public: - vector countBits(int num) { - vector ans(num + 1); - for (int i = 1; i <= num; ++i) ans[i] = 1 + ans[i - (i & -i)]; - return ans; - } -}; \ No newline at end of file diff --git a/leetcode/338. Counting Bits/s3.cpp b/leetcode/338. Counting Bits/s3.cpp deleted file mode 100644 index 67c32165..00000000 --- a/leetcode/338. Counting Bits/s3.cpp +++ /dev/null @@ -1,12 +0,0 @@ -// OJ: https://leetcode.com/problems/counting-bits -// Author: github.com/lzl124631x -// Time: O(N) -// Space: O(1) -class Solution { -public: - vector countBits(int num) { - vector ans(num + 1, 0); - for (int i = 1; i <= num; ++i) ans[i] = ans[i / 2] + (i % 2); - return ans; - } -}; \ No newline at end of file