post_href
stringlengths
57
213
python_solutions
stringlengths
71
22.3k
slug
stringlengths
3
77
post_title
stringlengths
1
100
user
stringlengths
3
29
upvotes
int64
-20
1.2k
views
int64
0
60.9k
problem_title
stringlengths
3
77
number
int64
1
2.48k
acceptance
float64
0.14
0.91
difficulty
stringclasses
3 values
__index_level_0__
int64
0
34k
https://leetcode.com/problems/palindrome-pairs/discuss/2129379/Python-solution-with-comments
class Solution: def palindromePairs(self, words: List[str]) -> List[List[int]]: def isPal(word): return word == word[::-1] palPairs = [] wordDict = {} for i, word in enumerate(words): wordDict[word] = i for i, word in enumerate(words): for j in range(0, len(word)): prefix, suffix = word[:j], word[j:] # find all suffix and prefix if prefix[::-1] in wordDict and wordDict[prefix[::-1]] != i and isPal(suffix): palPairs.append([i, wordDict[prefix[::-1]]]) # check if reverse prefix in dict and suffix palindrome if suffix[::-1] in wordDict and wordDict[suffix[::-1]] != i and isPal(prefix): palPairs.append([wordDict[suffix[::-1]], i]) # check if reverse suffix in dict and prefix palindrome if len(palPairs) > 0 and words[palPairs[-1][1]] == '': # tricky part for prefix or suffix is ''. for example, if '1' + '' is palindrome, then '' + '1' should also be palindrome. and add '' + '1' manually pair = palPairs[-1] palPairs.append([pair[1], pair[0]]) return palPairs
palindrome-pairs
Python solution with comments
asyoulikethemcallmetoo
0
192
palindrome pairs
336
0.352
Hard
5,800
https://leetcode.com/problems/house-robber-iii/discuss/872676/Python-3-or-DFS-Backtracking-or-Explanation
class Solution: def rob(self, root: TreeNode) -> int: def dfs(node): if not node: return 0, 0 left, right = dfs(node.left), dfs(node.right) v_take = node.val + left[1] + right[1] v_not_take = max(left) + max(right) return v_take, v_not_take return max(dfs(root))
house-robber-iii
Python 3 | DFS, Backtracking | Explanation
idontknoooo
5
439
house robber iii
337
0.539
Medium
5,801
https://leetcode.com/problems/house-robber-iii/discuss/872676/Python-3-or-DFS-Backtracking-or-Explanation
class Solution: def rob(self, root: TreeNode) -> int: def dfs(node): return (node.val + (left:=dfs(node.left))[1] + (right:=dfs(node.right))[1], max(left) + max(right)) if node else (0, 0) return max(dfs(root))
house-robber-iii
Python 3 | DFS, Backtracking | Explanation
idontknoooo
5
439
house robber iii
337
0.539
Medium
5,802
https://leetcode.com/problems/house-robber-iii/discuss/1529885/Well-Explained-and-Example-oror-Easy-to-understand-oror-DFS
class Solution: def rob(self, root: Optional[TreeNode]) -> int: def dfs(root): if root is None: return (0,0) left = dfs(root.left) right = dfs(root.right) return (root.val+left[1]+right[1], max(left)+max(right)) return max(dfs(root))
house-robber-iii
πŸ“ŒπŸ“Œ Well-Explained & Example || Easy-to-understand || DFS 🐍
abhi9Rai
2
161
house robber iii
337
0.539
Medium
5,803
https://leetcode.com/problems/house-robber-iii/discuss/566227/Python3-solution-with-Top-down-dp-on-Tree(with-explanation)
class Solution: def __init__(self): self.cache = {} def rob(self, root: TreeNode) -> int: return max(self.helper(root, 0), self.helper(root, 1)) def helper(self, root, status): # status == 0 means skip if not root: return 0 if (root,status) in self.cache: return self.cache[(root, status)] if status == 1: res = root.val + self.helper(root.left, 0) + self.helper(root.right, 0) else: res = max(self.helper(root.left,0), self.helper(root.left,1)) + max(self.helper(root.right,0), self.helper(root.right,1)) self.cache[(root, status)] = res return res
house-robber-iii
Python3 solution with Top-down dp on Tree(with explanation)
changpro
2
397
house robber iii
337
0.539
Medium
5,804
https://leetcode.com/problems/house-robber-iii/discuss/1613646/Python3-RECURSION-Explained
class Solution: def rob(self, root: Optional[TreeNode]) -> int: @cache def rec(root, withRoot): if not root: return 0 left, right = root.left, root.right res = 0 if withRoot: res = root.val + rec(left, False) + rec(right, False) else: res = max(rec(left, False), rec(left, True)) + max(rec(right, False), rec(right, True)) return res return max(rec(root, True), rec(root, False))
house-robber-iii
βœ”οΈ[Python3] RECURSION, Explained
artod
1
50
house robber iii
337
0.539
Medium
5,805
https://leetcode.com/problems/house-robber-iii/discuss/1449199/Python-Clean-Recursive-DFS-or-92.43-99.73
class Solution: def rob(self, root: Optional[TreeNode]) -> int: def dfs(node= root): if not node: return (0, 0) L, R = dfs(node.left), dfs(node.right) return (node.val+L[1]+R[1], max(L)+max(R)) return max(dfs())
house-robber-iii
[Python] Clean Recursive DFS | 92.43%, 99.73%
soma28
1
138
house robber iii
337
0.539
Medium
5,806
https://leetcode.com/problems/house-robber-iii/discuss/1437084/Using-DP-in-Python3-With-Picture-and-Explanation
class Solution: def rob(self, root: TreeNode) -> int: def DFS(root:TreeNode): res = [0, root.val] if root.left: temp = DFS(root.left) res[0] += max(temp) res[1] += temp[0] if root.right: temp = DFS(root.right) res[0] += max(temp) res[1] += temp[0] return res res = DFS(root) return max(res)
house-robber-iii
Using DP in Python3 With Picture and Explanation
GuoYunZheSE
1
60
house robber iii
337
0.539
Medium
5,807
https://leetcode.com/problems/house-robber-iii/discuss/2728281/python-dp-elegant-solution
class Solution: dp = {} def dfs(self,node,vis = False): if not node : return 0 if (node,vis) in self.dp: return self.dp[(node,vis)] if not vis: move1 = self.dfs(node.left,False) move2 = self.dfs(node.left,True) move3 = self.dfs(node.right,False) move4 = self.dfs(node.right,True) self.dp[(node,vis)] = max(move1+move3,move1+move4,move2+move3,move2+move4) else: self.dp[(node,vis)] = node.val + self.dfs(node.left,False) + self.dfs(node.right, False) return self.dp[(node,vis)] def rob(self, root: Optional[TreeNode]) -> int: return max(self.dfs(root),self.dfs(root,True))
house-robber-iii
python dp elegant solution
benon
0
10
house robber iii
337
0.539
Medium
5,808
https://leetcode.com/problems/house-robber-iii/discuss/2573064/python3-oror-Easy-to-understand-oror-DP
class Solution: def rob(self, root: Optional[TreeNode]) -> int: def dfs(node: Optional[TreeNode]): if not node: return 0, 0 left, pre_left = dfs(node.left) right, pre_right = dfs(node.right) return max(left+right, node.val+pre_left+pre_right), left+right left, pre_left = dfs(root.left) right, pre_right = dfs(root.right) return max(left+right, root.val+pre_left+pre_right)
house-robber-iii
python3 || Easy to understand || DP
victorchen1
0
47
house robber iii
337
0.539
Medium
5,809
https://leetcode.com/problems/house-robber-iii/discuss/2328568/Simple-Python-Solution-or-Memory-less-then-95.24-users
class Solution: def rob(self, root: Optional[TreeNode]) -> int: def dfs(node): if not node: return [0,0] left_pair = dfs(node.left) right_pair = dfs(node.right) with_root = node.val + left_pair[1] + right_pair[1] without_root = max(left_pair) + max(right_pair) return [with_root, without_root] return max(dfs(root))
house-robber-iii
Simple Python Solution | Memory less then 95.24 users
zip_demons
0
67
house robber iii
337
0.539
Medium
5,810
https://leetcode.com/problems/house-robber-iii/discuss/1771883/Python-oror-Recursion-oror-Easy
class Solution: def rob(self, root: Optional[TreeNode]) -> int: def robHouses(root): if root is None: return [0,0] l = robHouses(root.left) r = robHouses(root.right) return [root.val + l[1] + r[1], max(l) + max(r)] return max(robHouses(root))
house-robber-iii
Python || Recursion || Easy
kalyan_yadav
0
79
house robber iii
337
0.539
Medium
5,811
https://leetcode.com/problems/house-robber-iii/discuss/1612783/Python3-Dfs-solution
class Solution: def traversal(self, node): if not node: return 0, 0 l_child_val, l_prev_val = self.traversal(node.left) r_child_val, r_prev_val = self.traversal(node.right) return node.val + l_prev_val + r_prev_val, max(l_prev_val, l_child_val) + max(r_prev_val, r_child_val) def rob(self, root: TreeNode) -> int: # Imagine that there is a false root and its only child is the input root. fake_root.left == root and fake_root.right == None child_val, prev_val = self.traversal(root) return max(child_val, prev_val)
house-robber-iii
[Python3] Dfs solution
maosipov11
0
14
house robber iii
337
0.539
Medium
5,812
https://leetcode.com/problems/house-robber-iii/discuss/1612654/Python3-DFS.-beats-97
class Solution: def rob(self, root: Optional[TreeNode]) -> int: def dfs(node): """ input: node output: max score of this node, max score if not taking this node """ if not node: return 0, 0 lm, lnu = dfs(node.left) rm, rnu = dfs(node.right) cnu = lm + rm # taking maximum from childs, but not current node cm = max(node.val + rnu + lnu, cnu) # taking (current + not childs or `cnu` variant) as current max return cm, cnu return dfs(root)[0] ```
house-robber-iii
Python3 DFS. beats 97%
timetoai
0
16
house robber iii
337
0.539
Medium
5,813
https://leetcode.com/problems/house-robber-iii/discuss/1612584/python3-simple-and-intuitive-solution-brute-force-recursion-with-memoization
class Solution: def __init__(self): self.used = {} #used mark the node and its maximum possible value with root.val used self.unused = {} #unused marks the node and its maximum value with root.val unused def rob(self, root: Optional[TreeNode]) -> int: def helper(root, allow): #print(root) if not root:return 0 default = 0 #default mark the case that root.val unused if root in self.unused.keys(): default = self.unused[root] #memo else: default += helper(root.left, True) default += helper(root.right, True) self.unused[root] = default possible = 0 #possible stands for the case that root.val used, would be evaluated only if allow == True if allow: if root in self.used.keys(): possible = self.used[root] else: possible = root.val possible += helper(root.left, False) possible += helper(root.right, False) self.used[root] = possible return max(default, possible) return helper(root, True)
house-robber-iii
python3 simple and intuitive solution, brute force recursion with memoization
752937603
0
37
house robber iii
337
0.539
Medium
5,814
https://leetcode.com/problems/house-robber-iii/discuss/1541266/Python-DFS-O(n)-O(n)
class Solution: def rob(self, root: Optional[TreeNode]) -> int: if not root: return 0 def helper(root): if root.left is None and root.right is None: return (root.val, 0) left_child, left_grand_child = 0, 0 right_child, right_grand_child = 0, 0 if root.left: left_child, left_grand_child = helper(root.left) if root.right: right_child, right_grand_child = helper(root.right) max_child = left_child+right_child max_grand_child = root.val+left_grand_child+right_grand_child return max(max_child, max_grand_child), max_child child, grand_child = helper(root) return max(child, grand_child)
house-robber-iii
Python DFS O(n), O(n)
Kenfunkyourmama
0
93
house robber iii
337
0.539
Medium
5,815
https://leetcode.com/problems/house-robber-iii/discuss/815767/Python3-memoised-dfs-O(N)
class Solution: def rob(self, root: TreeNode) -> int: @lru_cache(None) def fn(node): """Return max money from sub-tree rooted at node.""" if not node: return 0 ans = node.val if node.left: ans += fn(node.left.left) + fn(node.left.right) if node.right: ans += fn(node.right.left) + fn(node.right.right) return max(ans, fn(node.left) + fn(node.right)) return fn(root)
house-robber-iii
[Python3] memoised dfs O(N)
ye15
0
97
house robber iii
337
0.539
Medium
5,816
https://leetcode.com/problems/counting-bits/discuss/466438/Python-clean-no-cheat-easy-to-understand.-Based-on-pattern.-Beats-90.
class Solution: def countBits(self, N: int) -> List[int]: stem = [0] while len(stem) < N+1: stem.extend([s + 1 for s in stem]) return stem[:N+1]
counting-bits
Python clean, no cheat, easy to understand. Based on pattern. Beats 90%.
kimonode
18
1,100
counting bits
338
0.753
Easy
5,817
https://leetcode.com/problems/counting-bits/discuss/1807954/python-O(n)-solution-using-dp
class Solution: def countBits(self, n: int) -> List[int]: res = [0] for i in range(1, n+1): res.append(res[i>>1] + i%2) return res
counting-bits
python O(n) solution using dp
hkwu6013
14
1,000
counting bits
338
0.753
Easy
5,818
https://leetcode.com/problems/counting-bits/discuss/1808775/Simple-Python-1-line
class Solution: def countBits(self, n: int) -> List[int]: return [(bin(i)[2:]).count("1") for i in range(n+1)]
counting-bits
Simple Python 1 line
khanter
5
163
counting bits
338
0.753
Easy
5,819
https://leetcode.com/problems/counting-bits/discuss/1810615/Python3-or-93-Faster-or-easy-to-understand
class Solution: def countBits(self, n: int) -> List[int]: result=[0]*(n+1) offset=1 # this will help to track pow(2,n) value ex: 1,2,4,8,16....... for i in range(1,n+1): if offset*2 ==i: offset=i # now we will add the no of 1's to ans result[i]=1+result[i-offset] return result
counting-bits
Python3 | 93% Faster | easy to understand
Anilchouhan181
3
182
counting bits
338
0.753
Easy
5,820
https://leetcode.com/problems/counting-bits/discuss/1808298/PythonororRecursive-and-Dynamic-Solution-oror-Detailed-ExplainationororO(n)
class Solution: def countBits(self, n: int) -> List[int]: res = [0]*(n+1) def bitcount(n): if n==0: return 0 if n==1: return 1 if n%2==0: return bitcount(n//2) else: return 1+ bitcount(n//2) for i in range(n+1): res[i]=count(i) return res
counting-bits
Python||Recursive and Dynamic Solution || Detailed Explaination||O(n)
ner0
3
114
counting bits
338
0.753
Easy
5,821
https://leetcode.com/problems/counting-bits/discuss/1808298/PythonororRecursive-and-Dynamic-Solution-oror-Detailed-ExplainationororO(n)
class Solution: def countBits(self, n: int) -> List[int]: res = [0]*(n+1) def count(n,memo): if n==0: return 0 if n==1: return 1 if memo[n]!=0 : return memo[n] if n%2==0: memo[n] = count(n//2,memo) return count(n//2,memo) else: memo[n] = 1+count(n//2,memo) return 1+count(n//2,memo) for i in range(n+1): res[i]=count(i,res) return res
counting-bits
Python||Recursive and Dynamic Solution || Detailed Explaination||O(n)
ner0
3
114
counting bits
338
0.753
Easy
5,822
https://leetcode.com/problems/counting-bits/discuss/1808298/PythonororRecursive-and-Dynamic-Solution-oror-Detailed-ExplainationororO(n)
class Solution: def countBits(self, n: int) -> List[int]: res = [0] for i in range(1,n+1): res.append(res[i>>1]+i%2) return res
counting-bits
Python||Recursive and Dynamic Solution || Detailed Explaination||O(n)
ner0
3
114
counting bits
338
0.753
Easy
5,823
https://leetcode.com/problems/counting-bits/discuss/1808298/PythonororRecursive-and-Dynamic-Solution-oror-Detailed-ExplainationororO(n)
class Solution(object): def countBits(self, n): return map(lambda i: bin(i).count('1'), range(n+1))
counting-bits
Python||Recursive and Dynamic Solution || Detailed Explaination||O(n)
ner0
3
114
counting bits
338
0.753
Easy
5,824
https://leetcode.com/problems/counting-bits/discuss/1260375/Python-DP-and-brute-force-solution-or-with-explanation
class Solution: def countBits(self, n: int) -> List[int]: ans = [] def binaryRepresentation(num): #method to change decial to binary number return bin(num).replace("0b","") for i in range(n+1): b = binaryRepresentation(i) ans.append(b.count("1")) return ans
counting-bits
[Python] DP and brute force solution | with explanation
arkumari2000
3
209
counting bits
338
0.753
Easy
5,825
https://leetcode.com/problems/counting-bits/discuss/1260375/Python-DP-and-brute-force-solution-or-with-explanation
class Solution: def countBits(self, n: int) -> List[int]: dp = [0,1,1,2] while len(dp)<n+1: dp.extend([num+1 for num in dp]) return dp[:n+1]
counting-bits
[Python] DP and brute force solution | with explanation
arkumari2000
3
209
counting bits
338
0.753
Easy
5,826
https://leetcode.com/problems/counting-bits/discuss/1824676/Python-Simple-and-Concise-Solutions
class Solution: def countBits(self, num): arr = [0] for i in range(1,num+1): arr.append(arr[i>>1]+(i&amp;1)) return arr
counting-bits
Python - Simple and Concise Solutions
domthedeveloper
2
246
counting bits
338
0.753
Easy
5,827
https://leetcode.com/problems/counting-bits/discuss/1824676/Python-Simple-and-Concise-Solutions
class Solution(object): def countBits(self, n): return map(self.hammingWeight, range(n+1)) def hammingWeight(self, n): count = 0 while n: count += n &amp; 1 n >>= 1 return count
counting-bits
Python - Simple and Concise Solutions
domthedeveloper
2
246
counting bits
338
0.753
Easy
5,828
https://leetcode.com/problems/counting-bits/discuss/1809359/Python-very-easy-solutions-or-Explained
class Solution: def countBits(self, n: int) -> List[int]: return [format(i,'b').count('1') for i in range(n+1)]
counting-bits
βœ… Python very easy solutions | Explained
dhananjay79
2
83
counting bits
338
0.753
Easy
5,829
https://leetcode.com/problems/counting-bits/discuss/1809359/Python-very-easy-solutions-or-Explained
class Solution: def countBits(self, n: int) -> List[int]: ans, prev = [0], 0 for i in range(1,n+1): if not i &amp; (i-1): prev = i ans.append(ans[i - prev] + 1) return ans
counting-bits
βœ… Python very easy solutions | Explained
dhananjay79
2
83
counting bits
338
0.753
Easy
5,830
https://leetcode.com/problems/counting-bits/discuss/1808938/Counting-Bits-or-Python-O(n)-Solution-or-95-Faster
class Solution: def countBits(self, n: int) -> List[int]: dp = [0] * (n+1) offset = 1 for i in range(1, n+1): if offset*2 == i: offset = i dp[i] = 1 + dp[i-offset] return dp
counting-bits
βœ”οΈ Counting Bits | Python O(n) Solution | 95% Faster
pniraj657
2
152
counting bits
338
0.753
Easy
5,831
https://leetcode.com/problems/counting-bits/discuss/1807984/Python3-oror-4-Methods-oror-Bit-Manip-Sucks
class Solution: def countBits(self, n: int) -> List[int]: res = [0] for i in range(1, n+1): res.append(res[i>>1] + i%2) return res
counting-bits
Python3 || 4 Methods || Bit Manip Sucks πŸ€·β€β™€οΈπŸ€·β€β™€οΈπŸ€·β€β™€οΈ
Dewang_Patil
2
36
counting bits
338
0.753
Easy
5,832
https://leetcode.com/problems/counting-bits/discuss/1807984/Python3-oror-4-Methods-oror-Bit-Manip-Sucks
class Solution: def countBits(self, n: int) -> List[int]: res = [] for i in range(n+1): b = '' while i != 0: b = str(i%2) + b i = i // 2 res.append(b.count('1')) return res
counting-bits
Python3 || 4 Methods || Bit Manip Sucks πŸ€·β€β™€οΈπŸ€·β€β™€οΈπŸ€·β€β™€οΈ
Dewang_Patil
2
36
counting bits
338
0.753
Easy
5,833
https://leetcode.com/problems/counting-bits/discuss/1807984/Python3-oror-4-Methods-oror-Bit-Manip-Sucks
class Solution: def countBits(self, n: int) -> List[int]: res = [] for i in range(n+1): res.append(bin(i).replace("0b", "").count('1')) return res
counting-bits
Python3 || 4 Methods || Bit Manip Sucks πŸ€·β€β™€οΈπŸ€·β€β™€οΈπŸ€·β€β™€οΈ
Dewang_Patil
2
36
counting bits
338
0.753
Easy
5,834
https://leetcode.com/problems/counting-bits/discuss/1807984/Python3-oror-4-Methods-oror-Bit-Manip-Sucks
class Solution: def countBits(self, n: int) -> List[int]: res = [] for i in range(n+1): t = "{0:b}".format(int(i)) res.append(t.count('1')) return res
counting-bits
Python3 || 4 Methods || Bit Manip Sucks πŸ€·β€β™€οΈπŸ€·β€β™€οΈπŸ€·β€β™€οΈ
Dewang_Patil
2
36
counting bits
338
0.753
Easy
5,835
https://leetcode.com/problems/counting-bits/discuss/1461004/Simple-Python-Solution
class Solution: def countBits(self, n: int) -> List[int]: a = [0]*(n+1) for i in range(n+1): a[i] = bin(i).count("1") return a
counting-bits
Simple Python Solution
Annushams
2
254
counting bits
338
0.753
Easy
5,836
https://leetcode.com/problems/counting-bits/discuss/1461004/Simple-Python-Solution
class Solution: def countBits(self, n: int) -> List[int]: return [bin(i).count("1") for i in range(n+1)]
counting-bits
Simple Python Solution
Annushams
2
254
counting bits
338
0.753
Easy
5,837
https://leetcode.com/problems/counting-bits/discuss/2290730/90-faster-Python
class Solution: def countBits(self, n: int) -> List[int]: dp = [0]*(n+1) offset = 1 for i in range(1,n+1): if offset*2 == i: offset = i dp[i] = 1+ dp[i-offset] return dp
counting-bits
90% faster Python
Abhi_009
1
109
counting bits
338
0.753
Easy
5,838
https://leetcode.com/problems/counting-bits/discuss/2219897/Python-solution-using-DP-or-Counting-Bits
class Solution: def countBits(self, n: int) -> List[int]: dp = [0] * (n + 1) offset = 1 for i in range(1,n+1): if offset * 2 == i: offset = i dp[i] = 1 + dp[i-offset] return dp
counting-bits
Python solution using DP | Counting Bits
nishanrahman1994
1
57
counting bits
338
0.753
Easy
5,839
https://leetcode.com/problems/counting-bits/discuss/1810947/simple-4-line-python-solution-O(logN)
class Solution: def countBits(self, n: int) -> List[int]: res = [0] while len(res) <= n: res.extend(list(map(lambda x: x+1, res))) return res[:n+1]
counting-bits
simple 4-line python solution O(logN)
VicV13
1
28
counting bits
338
0.753
Easy
5,840
https://leetcode.com/problems/counting-bits/discuss/1809151/Python-3-solution-without-converting-to-binary-form
class Solution: def countBits(self, n: int) -> List[int]: res = [0] while len(res) <= n: res.extend([j+1 for j in res]) return res[:n+1]
counting-bits
Python 3 solution without converting to binary form
j_a_y_v_a_r_d_h_a_n00
1
31
counting bits
338
0.753
Easy
5,841
https://leetcode.com/problems/counting-bits/discuss/1808747/Counting-Bits-or-Python-Easy-Solution-or-85-Faster
class Solution: def countBits(self, n: int) -> List[int]: res = [] for i in range(n+1): res.append(bin(i)[2:].count('1')) return res
counting-bits
Counting Bits | Python Easy Solution | 85% Faster
pniraj657
1
26
counting bits
338
0.753
Easy
5,842
https://leetcode.com/problems/counting-bits/discuss/1808715/Simple-Python-Solution
class Solution(object): def countBits(self, num): """ :type num: int :rtype: List[int] """ res=[0] for i in xrange(1,num+1): res.append(res[i>>1]+(i&amp;1)) return res
counting-bits
Simple Python Solution
user6774u
1
26
counting bits
338
0.753
Easy
5,843
https://leetcode.com/problems/counting-bits/discuss/1665838/Python-simple-solution
class Solution: def countBits(self, n: int) -> List[int]: dp = [0] for i in range(1, n + 1): dp.append(dp[i >> 1] + i % 2) return dp
counting-bits
Python simple solution
cause7ate9
1
154
counting bits
338
0.753
Easy
5,844
https://leetcode.com/problems/counting-bits/discuss/1647875/4-liner-beginner-friendly-python-program-using-.bin()-function-with-comments.
class Solution: def countBits(self, n: int) -> List[int]: #defining the answers array ans=[] for i in range(n+1): #converting number to binary and counting the total number of one's in the binary number and appending it to the answers array ans.append(bin(i)[2:].count('1')) return ans
counting-bits
4 liner beginner friendly python program using .bin() function with comments.
ebrahim007
1
88
counting bits
338
0.753
Easy
5,845
https://leetcode.com/problems/counting-bits/discuss/1276454/python-easy-to-understand
class Solution: def countBits(self, n: int) -> List[int]: result = [] for i in range(n+1): result.append(str(bin(i).replace("0b","")).count("1")) return result
counting-bits
python easy to understand
user2227R
1
61
counting bits
338
0.753
Easy
5,846
https://leetcode.com/problems/counting-bits/discuss/656691/Elegant-Small-Python-Solution-O(n)-Time-and-O(1)-Space
class Solution: def countBits(self, num: int) -> List[int]: res = [0] while (size := len(res)) < num+1: for i in range(size): if len(res) == num + 1: break res.append(res[i]+1) return res
counting-bits
Elegant, Small Python Solution - O(n) Time and O(1) Space
schedutron
1
115
counting bits
338
0.753
Easy
5,847
https://leetcode.com/problems/counting-bits/discuss/2844890/Python-or-One-Liner-or-92-Faster-or-Easy-Solution
class Solution: def countBits(self, n: int) -> List[int]: return [bin(i).count('1') for i in range(n+1)]
counting-bits
πŸ“Œ Python | One-Liner | 92% Faster | Easy Solution βœ”
atikfirat
0
1
counting bits
338
0.753
Easy
5,848
https://leetcode.com/problems/counting-bits/discuss/2844890/Python-or-One-Liner-or-92-Faster-or-Easy-Solution
class Solution: def countBits(self, n: int) -> List[int]: bin_list = [] for i in range(n + 1): bin_list.append(bin(i).count('1')) return bin_list
counting-bits
πŸ“Œ Python | One-Liner | 92% Faster | Easy Solution βœ”
atikfirat
0
1
counting bits
338
0.753
Easy
5,849
https://leetcode.com/problems/counting-bits/discuss/2842488/Python-or-Easy-Soultion-or-Binary-or-Strings
class Solution: def countBits(self, n: int) -> List[int]: ans = [] for i in range(n+1): ans.append(int(bin(i).split("b")[1].count('1'))) return ans
counting-bits
Python | Easy Soultion | Binary | Strings
atharva77
0
2
counting bits
338
0.753
Easy
5,850
https://leetcode.com/problems/counting-bits/discuss/2839120/Python-or-LeetCode-or-338.-Counting-Bits-(Easy)
class Solution: def countBits(self, n: int) -> List[int]: nums = [] for i in range(n + 1): k = bin(i) nums.append(k.count("1")) return nums
counting-bits
Python | LeetCode | 338. Counting Bits (Easy)
UzbekDasturchisiman
0
2
counting bits
338
0.753
Easy
5,851
https://leetcode.com/problems/counting-bits/discuss/2839120/Python-or-LeetCode-or-338.-Counting-Bits-(Easy)
class Solution: def countBits(self, n: int) -> List[int]: result=[0]*(n+1) offset=1 # this will help to track pow(2,n) value ex: 1,2,4,8,16....... for i in range(1,n+1): if offset*2 ==i: offset=i # now we will add the no of 1's to ans result[i]=1+result[i-offset] return result
counting-bits
Python | LeetCode | 338. Counting Bits (Easy)
UzbekDasturchisiman
0
2
counting bits
338
0.753
Easy
5,852
https://leetcode.com/problems/counting-bits/discuss/2839120/Python-or-LeetCode-or-338.-Counting-Bits-(Easy)
class Solution: def countBits(self, num: int) -> List[int]: dp = [0] for i in range(1, num + 1): if i % 2 == 1: dp.append(dp[i - 1] + 1) else: dp.append(dp[i // 2]) return dp
counting-bits
Python | LeetCode | 338. Counting Bits (Easy)
UzbekDasturchisiman
0
2
counting bits
338
0.753
Easy
5,853
https://leetcode.com/problems/counting-bits/discuss/2839120/Python-or-LeetCode-or-338.-Counting-Bits-(Easy)
class Solution: def countBits(self, num: int) -> List[int]: dp = [0] * (num + 1) offset = 1 for i in range(1, num + 1): if offset * 2 == i: offset = i dp[i] = 1 + dp[i - offset] return dp
counting-bits
Python | LeetCode | 338. Counting Bits (Easy)
UzbekDasturchisiman
0
2
counting bits
338
0.753
Easy
5,854
https://leetcode.com/problems/counting-bits/discuss/2824515/Python-code-easy-solution
class Solution: def countBits(self, n: int) -> List[int]: stack = [] i = 0 while i <= n: hold_binary = bin(i).replace("0b", "") counter = sum([1 for x in hold_binary if x == '1']) stack.append(counter) i += 1 return stack
counting-bits
Python code easy solution
nurawat126
0
4
counting bits
338
0.753
Easy
5,855
https://leetcode.com/problems/counting-bits/discuss/2814967/Imperative-Python-one-pass-solution
class Solution: def countBits(self, n: int) -> List[int]: output=[0]*(n+1) j=0 c=1 for i in range(1,n+1): output[i]=output[j]+1 j+=1 if(j==c): j=0 c=c<<1 return output
counting-bits
Imperative Python one pass solution
earthfail
0
6
counting bits
338
0.753
Easy
5,856
https://leetcode.com/problems/counting-bits/discuss/2801619/Simple-Python-solution
class Solution: def countBits(self, n: int) -> list[int]: blist = [] for i in range(n+1): blist.append(len(bin(i)[2:].replace('0', ''))) return blist
counting-bits
Simple Python solution
alangreg
0
4
counting bits
338
0.753
Easy
5,857
https://leetcode.com/problems/counting-bits/discuss/2772998/Python3.-95-faster-with-explanation-one-liner
class Solution: def countBits(self, n: int) -> List[int]: return [bin(i).count('1') for i in range(n + 1)]
counting-bits
Python3. 95% faster with explanation one liner
cvelazquez322
0
13
counting bits
338
0.753
Easy
5,858
https://leetcode.com/problems/counting-bits/discuss/2772998/Python3.-95-faster-with-explanation-one-liner
class Solution: def countBits(self, n: int) -> List[int]: rlist = [] for i in range(n + 1): rlist.append(bin(i).count('1')) return rlist
counting-bits
Python3. 95% faster with explanation one liner
cvelazquez322
0
13
counting bits
338
0.753
Easy
5,859
https://leetcode.com/problems/counting-bits/discuss/2770939/One-line-python-solution-(Runtime-802-ms-Beats-5.2-Memory-55.6-MB-Beats-30.22)
class Solution: def countBits(self, n: int) -> List[int]: return [sum(i) for i in [map(lambda x: int(x),list(f"{bin(i)}"[2:])) for i in range(n+1)]]
counting-bits
One line python solution (Runtime 802 ms Beats 5.2% Memory 55.6 MB Beats 30.22%)
skantrop
0
2
counting bits
338
0.753
Easy
5,860
https://leetcode.com/problems/counting-bits/discuss/2738800/Simple-Python-Code
class Solution: def countBits(self, n: int) -> List[int]: result=[0]*(n+1) offset=1 for i in range(1,n+1): if offset*2 ==i: offset=i result[i]=1+result[i-offset] return result
counting-bits
Simple Python Code
dnvavinash
0
2
counting bits
338
0.753
Easy
5,861
https://leetcode.com/problems/counting-bits/discuss/2704899/Python-Solution-or-One-Liner-or-96-Faster-or-Count-1s-and-Store
class Solution: def countBits(self, n: int) -> List[int]: return [bin(i).count("1") for i in range(n+1)]
counting-bits
Python Solution | One Liner | 96% Faster | Count 1s and Store
Gautam_ProMax
0
5
counting bits
338
0.753
Easy
5,862
https://leetcode.com/problems/counting-bits/discuss/2688327/Python-Super-Easy-and-Fast-Solution
class Solution: def countBits(self, n: int) -> List[int]: listCountBits = [0] for number in range(1, n+1): listCountBits.append(bin(number).count("1")) return listCountBits
counting-bits
Python Super Easy and Fast Solution
Heber_Alturria
0
3
counting bits
338
0.753
Easy
5,863
https://leetcode.com/problems/counting-bits/discuss/2586337/Python-%3A-DP
class Solution: def countBits(self, n: int) -> List[int]: dp = [0] * (n+1) c = 1 for i in range(1, n+1): if c * 2 == i : c = i dp[i] = 1 + dp[i-c] return dp
counting-bits
Python : DP βœ…
Khacker
0
53
counting bits
338
0.753
Easy
5,864
https://leetcode.com/problems/counting-bits/discuss/2543667/Python3-Solution-easy-to-understand
class Solution: def countBits(self, n: int) -> List[int]: n=n+1 ans=[] for i in range(n): temp=bin(i) temp=list(str(temp)) ans.append(temp.count('1')) return ans
counting-bits
Python3 Solution easy to understand
pranjalmishra334
0
50
counting bits
338
0.753
Easy
5,865
https://leetcode.com/problems/counting-bits/discuss/2541958/Simple-Python-Approach
class Solution: def countBits(self, n: int) -> List[int]: def cnt(n): sum = 0 for i in n: sum += int(i) return sum arr = [0] for i in range(1,n+1): arr.append(cnt(bin(i)[2:])) return arr
counting-bits
Simple Python Approach
betaal
0
42
counting bits
338
0.753
Easy
5,866
https://leetcode.com/problems/counting-bits/discuss/2530235/EASY-PYTHON3-SOLUTION
class Solution: def countBits(self, n: int) -> List[int]: ans = [] for i in range(n+1): ans.append(bin(i).count('1')) return ans
counting-bits
EASY PYTHON3 SOLUTION
rajukommula
0
19
counting bits
338
0.753
Easy
5,867
https://leetcode.com/problems/counting-bits/discuss/2408635/Python3-or-Need-Help-for-TLE-When-n-max(1*-105)
class Solution: def countBits(self, n: int) -> List[int]: #knowing the number of 1-bits in bin. rep of lower values of i than the current i #can have to solve current subproblem for i, since if i is odd, #number of 1 bits for i = number of 1 bits for previous number(even) + 1 #number of 1 bits for i if it is even and power of 2 = 1 #number of 1 bits for i if it not power of 2 but even = number of #largest powers of 2 even numbers I can use to sum up to i! #Ex. 6 = 4 + 2 -> used 2 powers of 2 = need 2 1-bits in bin. rep of 6! #Ex. 7 = 6 + 1 -> 6 uses 2 1-bits so 7 requires 2+1 = 3 1-bits in its bin. rep! #I showed with above examples that this problem demonstrates optimal substructure #property! -> Might be useful in bottom-up solve for lower values of state #parameter i and work your way in inc. order -> State parameter i corresponds #to each and every index of ans array length n+1! #also, we may need to refer to same number multiple times while #building up our solution -> Overlapping subproblem property satisfied! #Let me first attempt recursive approach! #I know I will face TLE so let's add dp memo for memoization! dp = [-1] * (n+1) #base cases dp[0] = 0 dp[1] = 1 #solve for subproblems ranging from 2 to the original input n! for i in range(2, n+1): #check if i is even and is power of 2! -> only require 1 1-bit! #if it is power of 2, taking bitwise and with itself and one less in value #bin. rep should produce all 0-bits1 if(i % 2 == 0 and (i &amp; i-1) == 0): dp[i] = 1 continue #last index i is odd case! -> It requires number of bits of prev. number, #which is even + additional 1-bit! if(i%2 != 0): dp[i] = dp[i-1] + 1 continue #last case: even number i not power of 2! else: #as long as we didn't reduce n! a = 2 ans = 0 while i: if((i - a) &amp; (i-a-1) == 0): i -= (n-a) a = 2 continue else: a += 2 dp[i] = ans #here the entire dp array will have for all 0<=i<=n, we will have number of #1-bits required for each i's binary representation! return dp
counting-bits
Python3 | Need Help for TLE When n= max(1* 10^5)
JOON1234
0
8
counting bits
338
0.753
Easy
5,868
https://leetcode.com/problems/counting-bits/discuss/2408343/Python3-or-Added-Memoization-for-Top-Down-Approach(Why-TLE)
class Solution: def countBits(self, n: int) -> List[int]: #knowing the number of 1-bits in bin. rep of lower values of i than the current i #can have to solve current subproblem for i, since if i is odd, #number of 1 bits for i = number of 1 bits for previous number(even) + 1 #number of 1 bits for i if it is even and power of 2 = 1 #number of 1 bits for i if it not power of 2 but even = number of #largest powers of 2 even numbers I can use to sum up to i! #Ex. 6 = 4 + 2 -> used 2 powers of 2 = need 2 1-bits in bin. rep of 6! #Ex. 7 = 6 + 1 -> 6 uses 2 1-bits so 7 requires 2+1 = 3 1-bits in its bin. rep! #I showed with above examples that this problem demonstrates optimal substructure #property! -> Might be useful in bottom-up solve for lower values of state #parameter i and work your way in inc. order -> State parameter i corresponds #to each and every index of ans array length n+1! #also, we may need to refer to same number multiple times while #building up our solution -> Overlapping subproblem property satisfied! #Let me first attempt recursive approach! #I know I will face TLE so let's add dp memo for memoization! dp = [-1] * (n+1) #base cases if(n == 0): return [0] if(n == 1): return [0, 1] #add a memo base case if(dp[n] != -1): return dp[n] #for n>1, array with at least 3 elements! #check if n is even and is power of 2! #if it is power of 2, taking bitwise and with itself and one less in value #bin. rep should produce all 0-bits1 if(n % 2 == 0 and (n &amp; n-1) == 0): #answer will be array from recursive call on n-1 plus the 1 1-bit required #for base 2 power even numbered n! dp[n] = self.countBits(n-1) + [1] return self.countBits(n-1) + [1] #last index n is odd case! if(n%2 != 0): recurse = self.countBits(n-1) num_bits_prev_num = recurse[-1] dp[n] = recurse + [num_bits_prev_num + 1] return recurse + [num_bits_prev_num + 1] #last case: even number n not power of 2! else: #as long as we didn't reduce n! i = 2 ans = 0 while n: if((n - i) &amp; (n-i-1) == 0): n -= (n-i) i = 2 continue else: i += 2 dp[n] = self.countBits(n-1) + [ans] return self.countBits(n-1) + [ans]
counting-bits
Python3 | Added Memoization for Top-Down Approach(Why TLE?)
JOON1234
0
25
counting bits
338
0.753
Easy
5,869
https://leetcode.com/problems/counting-bits/discuss/2320091/Count-of-Setbits-or-Python-or-80
class Solution: def countBits(self, n: int) -> List[int]: dp = [-1] * (n+1) dp[0] = 0 for i in range(n+1): if i%2 == 0: count = dp[i//2] else: count = 1 + dp[i//2] dp[i] = count return dp
counting-bits
Count of Setbits | Python | 80%
bliqlegend
0
45
counting bits
338
0.753
Easy
5,870
https://leetcode.com/problems/counting-bits/discuss/2258626/Python-Simple-Python-Solution
class Solution: def countBits(self, n: int) -> List[int]: if n == 0: return [0] ret = [0,1] if n == 1: return ret ret += [1]*(n-1) print([1]*(n-1)) print(ret) last_rank = 1 for i in range(2,n+1): if i == last_rank*2: last_rank = i else: ret[i] += ret[i-last_rank] return ret
counting-bits
[ Python ] βœ… Simple Python Solution βœ…βœ…
vaibhav0077
0
95
counting bits
338
0.753
Easy
5,871
https://leetcode.com/problems/counting-bits/discuss/2241099/Python-Dynamic-Programming-with-full-working-explanation
class Solution: def countBits(self, n: int) -> List[int]: # Time: O(n) and Space: O(n) dp = [0] * (n + 1) # we will need n+1 dynamic table to reuse values of previous integer from 0 to n(included) offset = 1 # starting with 1 for i in range(1, n + 1): # 0's will be zero automatically and if n=0 we will just return [0] if offset * 2 == i: # a new offset should be generated in binary when a number in multiples of 2 is found offset = i dp[i] = 1 + dp[i - offset] # binary values repeat itself in offset of multiples of 2 with just a minor change + 1 return dp
counting-bits
Python Dynamic Programming with full working explanation
DanishKhanbx
0
95
counting bits
338
0.753
Easy
5,872
https://leetcode.com/problems/counting-bits/discuss/2227827/Easy-python-solution
class Solution: def countBits(self, n: int) -> List[int]: ans = [] for i in range(n+1): b = bin(i) ans.append(str(b).count("1")) return ans
counting-bits
Easy python solution
knishiji
0
46
counting bits
338
0.753
Easy
5,873
https://leetcode.com/problems/counting-bits/discuss/2217873/Very-Easy-or-AND-or-Python3
class Solution: def countBits(self, n: int) -> List[int]: ans = [] for i in range(n+1): # self explainable count = self.ones(i) ans.append(count) return ans def ones(self, n): ans = 0 while n: # By ANDing a number with 1, I will get to know if the digit is a 0 or 1 # for example, 3 (011) # we start from the end i.e 1. We AND that 1 with 1, so we get a 1. Therefore ans is 1 in the first loop ans += n &amp; 1 # after checking the last digit, we need to check the second last digit, so we binary shift to the right using >> # So now it becomes 01 in te next loop n = n>>1 return ans
counting-bits
Very Easy | AND | Python3
abhinavcv007
0
13
counting bits
338
0.753
Easy
5,874
https://leetcode.com/problems/counting-bits/discuss/2206080/Simple-Python-Solution
class Solution: def countBits(self, n: int) -> List[int]: bit_list=[] for i in range(n+1): bit_list.append(bin(i).count("1")) return bit_list
counting-bits
Simple Python Solution
salsabilelgharably
0
49
counting bits
338
0.753
Easy
5,875
https://leetcode.com/problems/counting-bits/discuss/2162408/2-Approaches-oror-Dynamic-Programming-oror-TC-%3A-O(N)-oror-SC-%3A-O(N)
class Solution: def countBits(self, n: int) -> List[int]: if n <= 1: return [i for i in range(n+1)] dpArray = [-1]*(n+1) dpArray[0], dpArray[1], dpArray[2] = 0, 1, 1 i = 3 nearestPowerOfTwo = 2 while i < n + 1: if nearestPowerOfTwo*2 <= i: nearestPowerOfTwo *= 2 dpArray[i] = 1 + dpArray[i - nearestPowerOfTwo] i += 1 return dpArray
counting-bits
2 Approaches || Dynamic Programming || TC :- O(N) || SC :-O(N)
Vaibhav7860
0
68
counting bits
338
0.753
Easy
5,876
https://leetcode.com/problems/counting-bits/discuss/2130240/Faster-than-99.24-python3-solutions-Easy-to-Understand
class Solution: def countBits(self, n: int) -> List[int]: lstFin=[0]*(n+1) if n>0: lstFin[1]=1 def counter(n): if n<2: if n==1: return 1 else: return 0 else: if lstFin[n]>0: print(n,' ') return lstFin[n] if n%2==1: lstFin[n]+=1 p=n//2 lstFin[n]+=counter(p) for x in range(0,n+1): counter(x) return lstFin
counting-bits
Faster than 99.24% python3 solutions Easy to Understand
mukhter2
0
105
counting bits
338
0.753
Easy
5,877
https://leetcode.com/problems/counting-bits/discuss/2130240/Faster-than-99.24-python3-solutions-Easy-to-Understand
class Solution: def countBits(self, n: int) -> List[int]: lst=[] for x in range(0,n+1): if x>1: lst.append(lst[x//2]+(x%2)) else: lst.append(x) return lst
counting-bits
Faster than 99.24% python3 solutions Easy to Understand
mukhter2
0
105
counting bits
338
0.753
Easy
5,878
https://leetcode.com/problems/power-of-four/discuss/772261/Python3-1-liner-99.95-O(1)-explained
class Solution: def isPowerOfFour(self, num: int) -> bool: return num > 0 and not num &amp; (num - 1) and len(bin(num)) % 2
power-of-four
Python3 1 liner 99.95% O(1), explained
dangtrangiabao
9
546
power of four
342
0.458
Easy
5,879
https://leetcode.com/problems/power-of-four/discuss/2299478/Python-or-3-different-solutions-using-loop-recursion-and-bit-manipulation
class Solution: def isPowerOfFour(self, n: int) -> bool: # Solution 1 using recursion while n % 4 == 0 and n > 0: return self.isPowerOfFour(n/4) return n == 1 # Solution 2 iteration if n == 1: return True if n % 4: return False while n > 1: if n % 4: return False n //= 4 return n == 1 # Solution 3 using bit manipulation ''' Once we write numbers in it's binary representation, from there we can observe:=> i. 000001 , power of 2 and 4 ii. 000010, power of only 2 iii. 000100 , power of 2 and 4 iv. 001000, power of only 2 v. 010000 , power of 2 and 4 vi. 100000, power of only 2 We can see if the set bit is at an odd position and is a power of 2, it's also power of 4. ''' return n.bit_length() &amp; 1 and not(n &amp; (n-1))
power-of-four
Python | 3 different solutions using loop, recursion and bit-manipulation
__Asrar
2
100
power of four
342
0.458
Easy
5,880
https://leetcode.com/problems/power-of-four/discuss/1117905/Python-log4(n)-97-and-geared-solution-98.
class Solution: def isPowerOfThree(self, n: int) -> bool: epsilon = 0.0000000001 if not n > 0: return False logged = (math.log(abs(n), 4))%1 if (logged < epsilon or logged > 1 - epsilon): return True
power-of-four
[Python] log4(n) 97% and geared solution 98%.
larskl
2
226
power of four
342
0.458
Easy
5,881
https://leetcode.com/problems/power-of-four/discuss/1117905/Python-log4(n)-97-and-geared-solution-98.
class Solution: def isPowerOfThree(self, n: int) -> bool: while n%4==0 and n>=16: n = n/16 while n%4==0 and n>=4: n = n/4 if n == 1: return True
power-of-four
[Python] log4(n) 97% and geared solution 98%.
larskl
2
226
power of four
342
0.458
Easy
5,882
https://leetcode.com/problems/power-of-four/discuss/751479/Python3-summarizing-a-few-approaches
class Solution: def isPowerOfFour(self, num: int) -> bool: if num <= 0: return False while num: num, r = divmod(num, 4) if num and r: return False return r == 1
power-of-four
[Python3] summarizing a few approaches
ye15
2
112
power of four
342
0.458
Easy
5,883
https://leetcode.com/problems/power-of-four/discuss/751479/Python3-summarizing-a-few-approaches
class Solution: def isPowerOfFour(self, num: int) -> bool: if num <= 0: return False if num % 4: return num == 1 return self.isPowerOfFour(num//4)
power-of-four
[Python3] summarizing a few approaches
ye15
2
112
power of four
342
0.458
Easy
5,884
https://leetcode.com/problems/power-of-four/discuss/751479/Python3-summarizing-a-few-approaches
class Solution: def isPowerOfFour(self, num: int) -> bool: return num > 0 and (lambda x: x.count("1") == 1 and x.count("0")%2)(bin(num))
power-of-four
[Python3] summarizing a few approaches
ye15
2
112
power of four
342
0.458
Easy
5,885
https://leetcode.com/problems/power-of-four/discuss/751479/Python3-summarizing-a-few-approaches
class Solution: def isPowerOfFour(self, num: int) -> bool: return num > 0 and not (num &amp; (num-1)) and num &amp; 0x55555555
power-of-four
[Python3] summarizing a few approaches
ye15
2
112
power of four
342
0.458
Easy
5,886
https://leetcode.com/problems/power-of-four/discuss/751479/Python3-summarizing-a-few-approaches
class Solution: def isPowerOfFour(self, num: int) -> bool: return num > 0 and not (num &amp; (num-1)) and num.bit_length() &amp; 1
power-of-four
[Python3] summarizing a few approaches
ye15
2
112
power of four
342
0.458
Easy
5,887
https://leetcode.com/problems/power-of-four/discuss/2461434/Python-2-easy-methods
class Solution: def isPowerOfFour(self, n: int) -> bool: if n <= 0: return False if n == 1: return True if n % 4 != 0: return False return self.isPowerOfFour(n//4)
power-of-four
Python 2 easy methods
swaksh-war
1
145
power of four
342
0.458
Easy
5,888
https://leetcode.com/problems/power-of-four/discuss/2461434/Python-2-easy-methods
class Solution: def isPowerOfFour(self, n: int) -> bool: i = 0 curr = 0 while curr <= n: curr = 4 ** i if curr == n: return True i += 1 return False
power-of-four
Python 2 easy methods
swaksh-war
1
145
power of four
342
0.458
Easy
5,889
https://leetcode.com/problems/power-of-four/discuss/1338817/Simple-Python-Solution-for-%22Power-of-Four%22
class Solution: def isPowerOfFour(self, n: int) -> bool: if (n == 0): return False while (n != 1): if (n % 4 != 0): return False n = n // 4 return True
power-of-four
Simple Python Solution for "Power of Four"
sakshikhandare2527
1
167
power of four
342
0.458
Easy
5,890
https://leetcode.com/problems/power-of-four/discuss/1237905/WEEB-DOES-PYTHON-(ONE-LINER)
class Solution: def isPowerOfFour(self, n: int) -> bool: return n>0 and log(n,4) == int(log(n,4))
power-of-four
WEEB DOES PYTHON (ONE-LINER)
Skywalker5423
1
104
power of four
342
0.458
Easy
5,891
https://leetcode.com/problems/power-of-four/discuss/2846077/python3
class Solution: def isPowerOfFour(self, n: int) -> bool: # while not 0 and multiple of 4 while n and not n % 4: n //= 4 return n == 1
power-of-four
python3
wduf
0
1
power of four
342
0.458
Easy
5,892
https://leetcode.com/problems/power-of-four/discuss/2845475/python-solution
class Solution: def isPowerOfFour(self, n: int) -> bool: # second solution if n==1 : return True if n > 1 : return self.isPowerOfFour(n/4) return False # first solution if n <= 0 : return False if n == 1 : return True while n > 1 : if n%4 : return False else: n = n // 4 return True
power-of-four
python solution
Cosmodude
0
1
power of four
342
0.458
Easy
5,893
https://leetcode.com/problems/power-of-four/discuss/2841771/Python3-solution
class Solution: def isPowerOfFour(self, n: int) -> bool: if (n <= 0): return False if (4 ** round(log(n,4)) == n): return True return False
power-of-four
Python3 solution
SupriyaArali
0
2
power of four
342
0.458
Easy
5,894
https://leetcode.com/problems/power-of-four/discuss/2815616/Simple-Recursion
class Solution: def isPowerOfFour(self, n: int) -> bool: if n == 1: return True if n < 4: return False return self.isPowerOfFour(abs(n) / 4)
power-of-four
Simple Recursion
ZihanWang1819
0
1
power of four
342
0.458
Easy
5,895
https://leetcode.com/problems/power-of-four/discuss/2804900/Rather-fast-Python-solution-using-bitshift
class Solution: def isPowerOfFour(self, n: int) -> bool: if n <= 0: return False while n > 1: if n % 4 == 0: n >>= 2 else: return False return True
power-of-four
Rather fast Python solution using bitshift
alangreg
0
1
power of four
342
0.458
Easy
5,896
https://leetcode.com/problems/power-of-four/discuss/2776653/Python-oror-Recursion
class Solution: def isPowerOfFour(self, n: int) -> bool: if n == 1: return True elif n<1: return False return self.isPowerOfFour(n/4)
power-of-four
Python || Recursion
khoai345678
0
3
power of four
342
0.458
Easy
5,897
https://leetcode.com/problems/power-of-four/discuss/2738746/Simple-Python-Solution
class Solution: def isPowerOfFour(self, n: int) -> bool: if (n == 0): return False while (n != 1): if (n % 4 != 0): return False n = n // 4 return True
power-of-four
Simple Python Solution
dnvavinash
0
2
power of four
342
0.458
Easy
5,898
https://leetcode.com/problems/power-of-four/discuss/2682175/Python-Solution
class Solution: def isPowerOfFour(self, n: int) -> bool: if n==1: return True if n==0: return False while n != 1: if n%4 == 0: n = n /4 else: return False return True
power-of-four
Python Solution
Sheeza
0
1
power of four
342
0.458
Easy
5,899