Skip to content

Commit e9bd5d4

Browse files
committed
update 338
1 parent 203dbc0 commit e9bd5d4

File tree

3 files changed

+134
-137
lines changed

3 files changed

+134
-137
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ My personal leetcode answers<br/>
66
This is a **continually updated** open source project<br/>
77
<br/>
88
Total sovled: **363**<br/>
9-
Auto updated at: **2024-07-31 12:20:50**<br/>
9+
Auto updated at: **2024-07-31 12:41:29**<br/>
1010

1111
## 软件/Softwares
1212
- [Anki](https://apps.ankiweb.net/)
@@ -88,12 +88,13 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
8888
| [Leetcode-932. Beautiful Array](https://leetcode.com/problems/beautiful-array/description/) | [python](./leetcode/932.beautiful-array.py), [c++](./leetcode/932.beautiful-array.cpp) | O\(N\) | O\(N\) | dp | - |
8989

9090
## Bit Manipulation
91-
| Problem(6) | Solution | Time | Space | Note | Ref |
91+
| Problem(7) | Solution | Time | Space | Note | Ref |
9292
| ----- | ----- | ----- | ----- | ----- | ----- |
9393
| [Leetcode-67. Add Binary](https://leetcode.com/problems/add-binary/description/) | [c++](./leetcode/67.add-binary.cpp), [python](./leetcode/67.add-binary.py) | O\(M\+N\) | O\(1\) | \- | - |
9494
| [Leetcode-136. Single Number](https://leetcode.com/problems/single-number/description/) | [c++](./leetcode/136.single-number.cpp), [python](./leetcode/136.single-number.py) | O\(N\) | O\(1\) | \- | - |
9595
| [Leetcode-190. Reverse Bits](https://leetcode.com/problems/reverse-bits/description/) | [python](./leetcode/190.reverse-bits.py), [c++](./leetcode/190.reverse-bits.cpp) | O\(1\) | O\(1\) | \- | - |
9696
| [Leetcode-318. Maximum Product Of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/description/) | [c++](./leetcode/318.maximum-product-of-word-lengths.cpp), [python](./leetcode/318.maximum-product-of-word-lengths.py) | O\(N^2\) | O\(N\) | \- | - |
97+
| [Leetcode-338. Counting Bits](https://leetcode.com/problems/counting-bits/description/) | [python](./leetcode/338.counting-bits.py), [c++](./leetcode/338.counting-bits.cpp) | O\(N\) | O\(1\) | \- | - |
9798
| [Leetcode-342. Power Of Four](https://leetcode.com/problems/power-of-four/description/) | [python](./leetcode/342.power-of-four.py), [c++](./leetcode/342.power-of-four.cpp) | O\(1\) | O\(1\) | \- | - |
9899
| [Leetcode-461. Hamming Distance](https://leetcode.com/problems/hamming-distance/description/) | [python](./leetcode/461.hamming-distance.py), [c++](./leetcode/461.hamming-distance.cpp) | O\(1\) | O\(1\) | \- | - |
99100

@@ -234,7 +235,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
234235
| [Leetcode-3048. Earliest Second To Mark Indices I](https://leetcode.com/problems/earliest-second-to-mark-indices-i/description/) | [python](./leetcode/3048.earliest-second-to-mark-indices-i.py), [c++](./leetcode/3048.earliest-second-to-mark-indices-i.cpp) | O\(M\*logM\) | O\(M\) | \- | - |
235236

236237
## Dynamic Programming
237-
| Problem(39) | Solution | Time | Space | Note | Ref |
238+
| Problem(40) | Solution | Time | Space | Note | Ref |
238239
| ----- | ----- | ----- | ----- | ----- | ----- |
239240
| [Leetcode-10. Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/description/) | [python](./leetcode/10.regular-expression-matching.py), [c++](./leetcode/10.regular-expression-matching.cpp) | O\(MN\) | O\(MN\) | \- | - |
240241
| [Leetcode-53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/description/) | [python](./leetcode/53.maximum-subarray.py), [c++](./leetcode/53.maximum-subarray.cpp) | O\(N\) | O\(N\) | \- | - |
@@ -256,6 +257,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
256257
| [Leetcode-309. Best Time To Buy And Sell Stock With Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/) | [c++](./leetcode/309.best-time-to-buy-and-sell-stock-with-cooldown.cpp), [python](./leetcode/309.best-time-to-buy-and-sell-stock-with-cooldown.py) | O\(N\) | O\(N\) | \- | - |
257258
| [Leetcode-312. Burst Balloons](https://leetcode.com/problems/burst-balloons/description/) | [c++](./leetcode/312.burst-balloons.cpp), [python](./leetcode/312.burst-balloons.py) | O\(N^3\) | O\(N^2\) | \- | - |
258259
| [Leetcode-322. Coin Change](https://leetcode.com/problems/coin-change/description/) | [c++](./leetcode/322.coin-change.cpp), [python](./leetcode/322.coin-change.py) | O\(K \* N\) | O\(N\) | Index | [Video](https://youtu.be/EjMjlFjLRiM) |
260+
| [Leetcode-338. Counting Bits](https://leetcode.com/problems/counting-bits/description/) | [python](./leetcode/338.counting-bits.py), [c++](./leetcode/338.counting-bits.cpp) | O\(N\) | O\(1\) | \- | - |
259261
| [Leetcode-343. Integer Break](https://leetcode.com/problems/integer-break/description/) | [c++](./leetcode/343.integer-break.cpp), [python](./leetcode/343.integer-break.py) | O\(N\) | O\(N\) | \- | - |
260262
| [Leetcode-375. Guess Number Higher Or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii/description/) | [c++](./leetcode/375.guess-number-higher-or-lower-ii.cpp), [python](./leetcode/375.guess-number-higher-or-lower-ii.py) | O\(N^3\) | O\(N^2\) | DP\(Index\) | - |
261263
| [Leetcode-376. Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/description/) | [c++](./leetcode/376.wiggle-subsequence.cpp), [python](./leetcode/376.wiggle-subsequence.py) | O\(N\) | O\(N\) | \- | - |
@@ -385,7 +387,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
385387
| [Lintcode-1790. Rotate String II](https://www.lintcode.com/problem/rotate-string-ii) | [c++](./lintcode/1790.rotate-string-ii.cpp), [python](./lintcode/1790.rotate-string-ii.py) | O\(N\) | O\(N\) | Simulation | - |
386388

387389
## Other
388-
| Problem(226) | Solution | Time | Space | Note | Ref |
390+
| Problem(225) | Solution | Time | Space | Note | Ref |
389391
| ----- | ----- | ----- | ----- | ----- | ----- |
390392
| [Leetcode-1. Two Sum](https://leetcode.com/problems/two-sum/description/) | [python](./leetcode/1.two-sum.py), [c++](./leetcode/1.two-sum.cpp) | \- | \- | \- | - |
391393
| [Leetcode-2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/) | [python](./leetcode/2.add-two-numbers.py), [c++](./leetcode/2.add-two-numbers.cpp) | \- | \- | \- | - |
@@ -515,7 +517,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
515517
| [Leetcode-324. Wiggle Sort II](https://leetcode.com/problems/wiggle-sort-ii/description/) | [python](./leetcode/324.wiggle-sort-ii.py) | \- | \- | \- | - |
516518
| [Leetcode-328. Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/description/) | [python](./leetcode/328.odd-even-linked-list.py) | \- | \- | \- | - |
517519
| [Leetcode-336. Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/description/) | [python](./leetcode/336.palindrome-pairs.py) | \- | \- | \- | - |
518-
| [Leetcode-338. Counting Bits](https://leetcode.com/problems/counting-bits/description/) | [python](./leetcode/338.counting-bits.py), [c++](./leetcode/338.counting-bits.cpp) | \- | \- | \- | - |
519520
| [Leetcode-341. Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/description/) | [python](./leetcode/341.flatten-nested-list-iterator.py) | \- | \- | \- | - |
520521
| [Leetcode-344. Reverse String](https://leetcode.com/problems/reverse-string/description/) | [c++](./leetcode/344.reverse-string.cpp), [python](./leetcode/344.reverse-string.py) | \- | \- | \- | - |
521522
| [Leetcode-345. Reverse Vowels Of A String](https://leetcode.com/problems/reverse-vowels-of-a-string/description/) | [c++](./leetcode/345.reverse-vowels-of-a-string.cpp), [python](./leetcode/345.reverse-vowels-of-a-string.py) | \- | \- | \- | - |

leetcode/338.counting-bits.cpp

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,78 @@
1-
/*
2-
* @lc app=leetcode id=338 lang=cpp
3-
*
4-
* [338] Counting Bits
5-
*
6-
* https://leetcode.com/problems/counting-bits/description/
7-
*
8-
* algorithms
9-
* Medium (63.30%)
10-
* Total Accepted: 167.6K
11-
* Total Submissions: 259.1K
12-
* Testcase Example: '2'
13-
*
14-
* Given a non negative integer number num. For every numbers i in the range 0
15-
* ≤ i ≤ num calculate the number of 1's in their binary representation and
16-
* return them as an array.
17-
*
18-
* Example 1:
19-
*
20-
*
21-
* Input: 2
22-
* Output: [0,1,1]
23-
*
24-
* Example 2:
25-
*
26-
*
27-
* Input: 5
28-
* Output: [0,1,1,2,1,2]
29-
*
30-
*
31-
* Follow up:
32-
*
33-
*
34-
* It is very easy to come up with a solution with run time
35-
* O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a
36-
* single pass?
37-
* Space complexity should be O(n).
38-
* Can you do it like a boss? Do it without using any builtin function like
39-
* __builtin_popcount in c++ or in any other language.
40-
*
41-
*/
1+
// Tag: Dynamic Programming, Bit Manipulation
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// 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.
8+
//  
9+
// Example 1:
10+
//
11+
// Input: n = 2
12+
// Output: [0,1,1]
13+
// Explanation:
14+
// 0 --> 0
15+
// 1 --> 1
16+
// 2 --> 10
17+
//
18+
// Example 2:
19+
//
20+
// Input: n = 5
21+
// Output: [0,1,1,2,1,2]
22+
// Explanation:
23+
// 0 --> 0
24+
// 1 --> 1
25+
// 2 --> 10
26+
// 3 --> 11
27+
// 4 --> 100
28+
// 5 --> 101
29+
//
30+
//  
31+
// Constraints:
32+
//
33+
// 0 <= n <= 105
34+
//
35+
//  
36+
// Follow up:
37+
//
38+
// It is very easy to come up with a solution with a runtime of O(n log n). Can you do it in linear time O(n) and possibly in a single pass?
39+
// Can you do it without using any built-in function (i.e., like __builtin_popcount in C++)?
40+
//
41+
//
42+
4243
class Solution {
4344
public:
44-
vector<int> countBits(int num) {
45-
vector<int> res(num + 1, 0);
46-
for (int i = 1; i < num + 1; i++) {
47-
res[i] = res[i & (i - 1)] + 1;
45+
vector<int> countBits(int n) {
46+
vector<int> res(n + 1, 0);
47+
for (int i = 1; i <= n; i++) {
48+
int n = i;
49+
while (n > 0) {
50+
res[i] += 1;
51+
n = n & (n - 1);
52+
}
4853
}
49-
5054
return res;
5155
}
5256
};
5357

54-
55-
56-
//f(x) = f(x >> 1) + x % 2
57-
class Solution2 {
58+
class Solution {
5859
public:
59-
vector<int> countBits(int num) {
60-
vector<int> table;
61-
62-
for(auto i = 0; i <= num; ++i)
63-
{
64-
table.push_back(i % 2);
65-
66-
int pre = i >> 1;
67-
if (pre > 0)
68-
{
69-
table[i] += table[pre];
70-
}
60+
vector<int> countBits(int n) {
61+
vector<int> dp(n + 1, 0);
62+
for (int i = 1; i <= n; i++) {
63+
dp[i] = dp[i & (i - 1)] + 1;
7164
}
72-
73-
return table;
65+
return dp;
7466
}
7567
};
68+
69+
class Solution {
70+
public:
71+
vector<int> countBits(int n) {
72+
vector<int> dp(n + 1, 0);
73+
for (int i = 1; i <= n; i++) {
74+
dp[i] = dp[i >> 1] + (i & 1);
75+
}
76+
return dp;
77+
}
78+
};

leetcode/338.counting-bits.py

Lines changed: 61 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,65 @@
1-
#
2-
# @lc app=leetcode id=338 lang=python
3-
#
4-
# [338] Counting Bits
5-
#
6-
# https://leetcode.com/problems/counting-bits/description/
7-
#
8-
# algorithms
9-
# Medium (63.30%)
10-
# Total Accepted: 167.6K
11-
# Total Submissions: 259.1K
12-
# Testcase Example: '2'
13-
#
14-
# Given a non negative integer number num. For every numbers i in the range 0 ≤
15-
# i ≤ num calculate the number of 1's in their binary representation and return
16-
# them as an array.
17-
#
18-
# Example 1:
19-
#
20-
#
21-
# Input: 2
22-
# Output: [0,1,1]
23-
#
24-
# Example 2:
25-
#
26-
#
27-
# Input: 5
28-
# Output: [0,1,1,2,1,2]
29-
#
30-
#
31-
# Follow up:
32-
#
33-
#
34-
# It is very easy to come up with a solution with run time
35-
# O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a
36-
# single pass?
37-
# Space complexity should be O(n).
38-
# Can you do it like a boss? Do it without using any builtin function like
39-
# __builtin_popcount in c++ or in any other language.
40-
#
41-
#
42-
class Solution(object):
43-
def countBits(self, num):
44-
"""
45-
:type num: int
46-
:rtype: List[int]
47-
"""
48-
49-
res = [0 for i in range(num + 1)]
1+
# Tag: Dynamic Programming, Bit Manipulation
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
506

51-
for i in range(1, num + 1):
52-
res[i] = res[i & (i - 1)] + 1
7+
# 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.
8+
#  
9+
# Example 1:
10+
#
11+
# Input: n = 2
12+
# Output: [0,1,1]
13+
# Explanation:
14+
# 0 --> 0
15+
# 1 --> 1
16+
# 2 --> 10
17+
#
18+
# Example 2:
19+
#
20+
# Input: n = 5
21+
# Output: [0,1,1,2,1,2]
22+
# Explanation:
23+
# 0 --> 0
24+
# 1 --> 1
25+
# 2 --> 10
26+
# 3 --> 11
27+
# 4 --> 100
28+
# 5 --> 101
29+
#
30+
#  
31+
# Constraints:
32+
#
33+
# 0 <= n <= 105
34+
#
35+
#  
36+
# Follow up:
37+
#
38+
# It is very easy to come up with a solution with a runtime of O(n log n). Can you do it in linear time O(n) and possibly in a single pass?
39+
# Can you do it without using any built-in function (i.e., like __builtin_popcount in C++)?
40+
#
41+
#
5342

43+
class Solution:
44+
def countBits(self, n: int) -> List[int]:
45+
res = [0 for i in range(n + 1)]
46+
for i in range(1, n + 1):
47+
n = i
48+
while n > 0:
49+
res[i] += 1
50+
n = n & (n - 1)
5451
return res
55-
56-
# f(x) = f(x >> 1) + x % 2
57-
class Solution2(object):
58-
def countBits(self, num):
59-
"""
60-
:type num: int
61-
:rtype: List[int]
62-
"""
63-
table = []
64-
65-
for i in range(num + 1):
66-
table.append(i % 2)
67-
pre = i >> 1
68-
69-
if pre > 0:
70-
table[i] += table[pre]
7152

72-
return table
53+
class Solution:
54+
def countBits(self, n: int) -> List[int]:
55+
dp = [0 for i in range(n + 1)]
56+
for i in range(1, n + 1):
57+
dp[i] = dp[i & (i - 1)] + 1
58+
return dp
59+
60+
class Solution:
61+
def countBits(self, n: int) -> List[int]:
62+
dp = [0 for i in range(n + 1)]
63+
for i in range(1, n + 1):
64+
dp[i] = dp[i >> 1] + (i & 1)
65+
return dp

0 commit comments

Comments
 (0)