
g0301_0400.s0338_counting_bits.Solution.py Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
# #Easy #Dynamic_Programming #Bit_Manipulation #Udemy_Bit_Manipulation
# #Big_O_Time_O(num)_Space_O(num) #2024_06_08_Time_55_ms_(88.80%)_Space_23_MB_(95.35%)
class Solution:
def countBits(self, num: int) -> List[int]:
result = [0] * (num + 1)
borderPos = 1
incrPos = 1
for i in range(1, len(result)):
if incrPos == borderPos:
result[i] = 1
incrPos = 1
borderPos = i
else:
result[i] = 1 + result[incrPos]
incrPos += 1
return result
© 2015 - 2025 Weber Informatics LLC | Privacy Policy