Skip to content

Commit 16ef2d0

Browse files
solves counting bits
1 parent 5287d6a commit 16ef2d0

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-161/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-161/2081-1abc9c.svg)
5-
![problems-solved-python](https://img.shields.io/badge/Python-161/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-162/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-162/2081-1abc9c.svg)
5+
![problems-solved-python](https://img.shields.io/badge/Python-162/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
88

@@ -94,6 +94,7 @@
9494
| 299 | [Bulls and Cows](https://leetcode.com/problems/bulls-and-cows) | [![Java](assets/java.png)](src/BullsAndCows.java) [![Python](assets/python.png)](python/bulls_and_cows.py) |
9595
| 303 | [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable) | [![Java](assets/java.png)](src/RangeSumQueryImmutable.java) [![Python](assets/python.png)](python/range_sum_query_immutable.py) |
9696
| 326 | [Power of Three](https://leetcode.com/problems/power-of-three) | [![Java](assets/java.png)](src/PowerOfThree.java) [![Python](assets/python.png)](python/power_of_three.py) |
97+
| 326 | [Counting Bits](https://leetcode.com/problems/counting-bits/) | [![Java](assets/java.png)](src/CountingBits.java) [![Python](assets/python.png)](python/counting_bits.py) |
9798
| 339 | 🔒 [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum) | |
9899
| 342 | [Power of Four](https://leetcode.com/problems/power-of-four) | [![Java](assets/java.png)](src/PowerOf4.java) [![Python](assets/python.png)](python/power_of_four.py) |
99100
| 344 | [Reverse A String](https://leetcode.com/problems/reverse-string) | [![Java](assets/java.png)](src/ReverseString.java) [![Python](assets/python.png)](python/reverse_a_string.py) |

python/counting_bits.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def countBits(self, n: int) -> List[int]:
6+
result = [0] * (n + 1)
7+
if len(result) > 1: result[1] = 1
8+
i, k = 0, 2
9+
while k < len(result):
10+
i = 0
11+
while i < k and i + k < len(result):
12+
result[i + k] = 1 + result[i]
13+
i += 1
14+
k *= 2
15+
return result

src/CountingBits.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class CountingBits {
2+
public static int[] countBits(int n) {
3+
int[] result = new int[n + 1];
4+
if (result.length > 1) result[1] = 1;
5+
for (int k = 2 ; k < result.length ; k *= 2) {
6+
for (int i = 0 ; i < k && i + k < result.length; i++) {
7+
result[k + i] = 1 + result[i];
8+
}
9+
}
10+
return result;
11+
}
12+
}

0 commit comments

Comments
 (0)