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/majority-element-ii/discuss/2215528/Python-Simple-solution
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: decider = len((nums))//3 d = {} op = [] for x in nums: if x not in d: d[x] = 1 else: d[x] += 1 for x, y in d.items(): if y > decider: op.append(x) return op
majority-element-ii
[Python] Simple solution
sushil021996
1
31
majority element ii
229
0.442
Medium
4,200
https://leetcode.com/problems/majority-element-ii/discuss/2184491/Python3-Easy-O(n)-Solution-Counter-Faster-than-84.98
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: limit = len(nums) // 3 c = Counter(nums) fin = [] for key, val in c.items(): if val > limit: fin.append(key) return fin
majority-element-ii
Python3 Easy O(n) Solution Counter, Faster than 84.98%
orebitt
1
131
majority element ii
229
0.442
Medium
4,201
https://leetcode.com/problems/majority-element-ii/discuss/2184491/Python3-Easy-O(n)-Solution-Counter-Faster-than-84.98
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: limit = len(nums) // 3 c = Counter(nums) return [key for key, val in c.items() if val > limit]
majority-element-ii
Python3 Easy O(n) Solution Counter, Faster than 84.98%
orebitt
1
131
majority element ii
229
0.442
Medium
4,202
https://leetcode.com/problems/majority-element-ii/discuss/2184491/Python3-Easy-O(n)-Solution-Counter-Faster-than-84.98
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: return [key for key, val in Counter(nums).items() if val > len(nums) // 3]
majority-element-ii
Python3 Easy O(n) Solution Counter, Faster than 84.98%
orebitt
1
131
majority element ii
229
0.442
Medium
4,203
https://leetcode.com/problems/majority-element-ii/discuss/1876568/Python-solution-one-liner-memory-less-than-98
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: return [x for x in set(nums) if nums.count(x) > len(nums) / 3]
majority-element-ii
Python solution one liner, memory less than 98%
alishak1999
1
90
majority element ii
229
0.442
Medium
4,204
https://leetcode.com/problems/majority-element-ii/discuss/1787659/Python-solution-using-Hashmap
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: memory = {} elem = [] if len(nums) ==1: return nums for num in nums: if num in memory: memory[num] = memory.get(num, 0) + 1 else: memory[num] = 1 for k in memory: if memory[k]>len(nums)//3: elem.append(k) return elem
majority-element-ii
Python solution using Hashmap
a_knotty_mathematician
1
55
majority element ii
229
0.442
Medium
4,205
https://leetcode.com/problems/majority-element-ii/discuss/858823/Python-One-Line-oror-Easy
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: return [num for num in set(nums) if nums.count(num) > len(nums)//3]
majority-element-ii
Python One Line || Easy
airksh
1
109
majority element ii
229
0.442
Medium
4,206
https://leetcode.com/problems/majority-element-ii/discuss/2847807/Simple-python-solution-(89.64)-faster
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: s=Counter(nums) res=[] for i in s: if s[i]>len(nums)//3: res.append(i) return res
majority-element-ii
Simple python solution (89.64%) faster
fahim_ash
0
1
majority element ii
229
0.442
Medium
4,207
https://leetcode.com/problems/majority-element-ii/discuss/2806587/Python-O(1)-space-solution
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: nums.sort() threshold = len(nums)//3 res = [] i = 0 j = 0 while i<len(nums): while j<len(nums) and nums[i]==nums[j]: j+=1 if (j-i)>threshold: res+=[nums[i]] i = j return res
majority-element-ii
Python O(1) space solution
h3030666
0
1
majority element ii
229
0.442
Medium
4,208
https://leetcode.com/problems/majority-element-ii/discuss/2806159/hashmap-easy-python
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: frq={} n=len(nums) res=[] for num in nums: if num in frq: frq[num]+=1 else: frq[num]=1 for num in frq: if frq[num]> n//3: res.append(num) return res
majority-element-ii
hashmap easy-python
atulraze
0
2
majority element ii
229
0.442
Medium
4,209
https://leetcode.com/problems/majority-element-ii/discuss/2786096/Pythonor-Easy-To-Understand-or-Step-By-Step-or-Brute-Force-or-Optimal-or
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: n=len(nums) mapp=dict() for i in nums: if i not in mapp: mapp[i]=1 else: mapp[i]+=1 return [key for key,value in mapp.items() if value>n//3]
majority-element-ii
Python| Easy To Understand | Step By Step | Brute Force | Optimal |
varun21vaidya
0
9
majority element ii
229
0.442
Medium
4,210
https://leetcode.com/problems/majority-element-ii/discuss/2786096/Pythonor-Easy-To-Understand-or-Step-By-Step-or-Brute-Force-or-Optimal-or
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: candidate1=0 candidate2=0 count1=0 count2=0 for value in nums: if candidate1==value: count1+=1 elif candidate2==value: count2+=1 elif count1==0: candidate1=value count1=1 elif count2==0: candidate2=value count2=1 else: count1-=1 count2-=1 # now we may have got 2 candidates but # they do not neccessarily have > n//3 count # so we check their count and append to res # only if count is >n//3 newcount1=0 newcount2=0 for count in nums: if candidate1==count: newcount1+=1 elif candidate2==count: newcount2+=1 res=[] if newcount1>n//3: res.append(candidate1) if newcount2>n//3: res.append(candidate2) return res
majority-element-ii
Python| Easy To Understand | Step By Step | Brute Force | Optimal |
varun21vaidya
0
9
majority element ii
229
0.442
Medium
4,211
https://leetcode.com/problems/majority-element-ii/discuss/2780356/Using-dict
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: occr = {} for i in nums: occr[i] = occr.get(i,0) +1 return [k for k,v in occr.items() if v > len(nums)/3]
majority-element-ii
Using dict
MayuD
0
1
majority element ii
229
0.442
Medium
4,212
https://leetcode.com/problems/majority-element-ii/discuss/2728474/Concise-Python-and-Golang-Solution
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: nums_set = set(nums) appear_times = {} for num in nums_set: appear_times[num] = nums.count(num) result = [] nums_division_3 = len(nums) / 3 for number, appear_time in appear_times.items(): if nums_division_3 < appear_time: result.append(number) return result
majority-element-ii
Concise Python and Golang Solution
namashin
0
3
majority element ii
229
0.442
Medium
4,213
https://leetcode.com/problems/majority-element-ii/discuss/2684828/Python3-Solution-oror-O(N)-Time-and-O(1)-Space-Complexity
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: value1,value2,count1,count2 = None, None,0,0 n=int(len(nums)/3) for i in nums: if i==value1: count1+=1 elif i==value2: count2+=1 elif count1==0: count1,value1=1,i elif count2==0: count2,value2=1,i else: count1,count2=count1-1,count2-1 itr1,itr2 = 0,0 for i in nums: if i==value1: itr1+=1 elif i==value2: itr2+=1 array=[] if itr1>n: array.append(value1) if itr2>n: array.append(value2) return array
majority-element-ii
Python3 Solution || O(N) Time & O(1) Space Complexity
akshatkhanna37
0
2
majority element ii
229
0.442
Medium
4,214
https://leetcode.com/problems/majority-element-ii/discuss/2639068/Python-oror-Easily-Understood-oror-Faster-than-90-oror-Fast-linear-time-using-counter
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: a = collections.Counter(nums) ans = [] for i in a.items(): if i[1]>len(nums)//3: ans.append(i[0]) return ans
majority-element-ii
πŸ”₯ Python || Easily Understood βœ… || Faster than 90% || Fast linear time using counter
rajukommula
0
4
majority element ii
229
0.442
Medium
4,215
https://leetcode.com/problems/majority-element-ii/discuss/2636498/Python-98-Faster-Easy-to-follow
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: #Output list o_list = [] #unique nums u_nums = list(set(nums)) #set the divisor for checking n_check = len(nums)/3 #counts the numbers in original list for n in u_nums: if nums.count(n) > n_check: o_list.append(n) return(o_list)
majority-element-ii
Python 98% Faster - Easy to follow
ovidaure
0
15
majority element ii
229
0.442
Medium
4,216
https://leetcode.com/problems/majority-element-ii/discuss/2622272/python3-One-liner
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: return ([x for x in list(set(nums)) if nums.count(x) > len(nums)/3])
majority-element-ii
python3 One liner
priyam_jsx
0
29
majority element ii
229
0.442
Medium
4,217
https://leetcode.com/problems/majority-element-ii/discuss/2604185/Majority-Element-II
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: d = {} for i in range(len(nums)): if nums[i] not in d: d[nums[i]] = 1 else: d[nums[i]] += 1 temp = [] for k,v in d.items(): if v > len(nums)/3: temp.append(k) return temp
majority-element-ii
Majority Element II
PravinBorate
0
58
majority element ii
229
0.442
Medium
4,218
https://leetcode.com/problems/majority-element-ii/discuss/2490237/faster-than-90.06-of-Python3
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: ans = [] for i in set(nums): if nums.count(i) > len(nums)//3: ans.append(i) return ans
majority-element-ii
faster than 90.06% of Python3
EbrahimMG
0
64
majority element ii
229
0.442
Medium
4,219
https://leetcode.com/problems/majority-element-ii/discuss/2444239/Easy-Solution-Using-Hashmap
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: list1=[] from collections import Counter cn=Counter(nums) for key,value in cn.items(): if value>len(nums)//3: list1.append(key) return list1
majority-element-ii
Easy Solution Using Hashmap
deepanshu704281
0
16
majority element ii
229
0.442
Medium
4,220
https://leetcode.com/problems/majority-element-ii/discuss/2327491/Hashmap-o(n)-or-Python
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: hashmap=dict() res=[] for i in nums: if i not in hashmap: hashmap[i]=1 else: hashmap[i]+=1 for i,j in hashmap.items(): if j>math.floor(len(nums)/3): res.append(i) return res
majority-element-ii
Hashmap o(n) | Python
cse_190310106
0
62
majority element ii
229
0.442
Medium
4,221
https://leetcode.com/problems/majority-element-ii/discuss/2295160/PythonororEasy-to-understand-code
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: cnt = len(nums)//3 if cnt == 0: return list(set(nums)) res = [] for i in range(len(nums)): if nums[i] in res: continue if nums.count(nums[i]) > cnt: res.append(nums[i]) return res
majority-element-ii
Python||Easy to understand code
sk4213
0
25
majority element ii
229
0.442
Medium
4,222
https://leetcode.com/problems/majority-element-ii/discuss/2207045/5-lines-simple-python-code
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: result = [] for num in set(nums): if len(nums)/3 < nums.count(num): result.append(num) return result
majority-element-ii
5 lines simple python code
Sumit_agrawal
0
39
majority element ii
229
0.442
Medium
4,223
https://leetcode.com/problems/majority-element-ii/discuss/2173746/Python-simple-solution
class Solution: def majorityElement(self, nums: List[int]) -> int: ans = [] for i in set(nums): if nums.count(i) > len(nums)/3: ans.append(i) return ans
majority-element-ii
Python simple solution
StikS32
0
40
majority element ii
229
0.442
Medium
4,224
https://leetcode.com/problems/majority-element-ii/discuss/2167226/Python3-or-Counter-then-return-the-list-or-self-explanatory
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: cnt = Counter(nums) return [k for k, v in cnt.items() if cnt[k] > (len(nums)//3)]
majority-element-ii
Python3 | Counter then return the list | self-explanatory
Ploypaphat
0
22
majority element ii
229
0.442
Medium
4,225
https://leetcode.com/problems/majority-element-ii/discuss/2142686/Python-Boyer-Moore-Voting-Algorithm-Solution
class Solution: def majorityElement(self, nums): cand1, cand2 = self.get_candidates(nums) # return [x for x in (cand1, cand2) if nums.count(x) > len(nums) // 3] # Get the candidate no of occurrences count1, count2 = 0, 0 for num in nums: count1 += 1 if num == cand1 else 0 count2 += 1 if num == cand2 else 0 # Verify if indeed they have occurrences greate than n/3 ans = [] athird = len(nums) // 3 ans += [cand1] if count1 > athird else [] ans += [cand2] if count2 > athird else [] return ans def get_candidates(self, nums): cand1, count1 = None, 0 cand2, count2 = None, 0 for num in nums: if num == cand1: count1 += 1 elif num == cand2: count2 += 1 elif count1 == 0: cand1, count1 = num, 1 elif count2 == 0: cand2, count2 = num, 1 else: count1, count2 = count1 - 1, count2 - 1 return cand1, cand2
majority-element-ii
Python Boyer Moore Voting Algorithm Solution
ErickMwazonga
0
47
majority element ii
229
0.442
Medium
4,226
https://leetcode.com/problems/majority-element-ii/discuss/2135183/python2-O(1)-space-solution
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: n = len(nums) if not nums: return [] cand1, count1, cand2, count2 = None, 0, None, 0 for i in nums: if cand1==i: count1+=1 elif cand2==i: count2+=1 elif count1==0: cand1=i count1+=1 elif count2==0: cand2=i count2+=1 else: count1-=1 count2-=1 return [i for i in [cand1, cand2] if nums.count(i)>n//3]
majority-element-ii
python2 O(1) space solution
prashantpandey9
0
35
majority element ii
229
0.442
Medium
4,227
https://leetcode.com/problems/majority-element-ii/discuss/2108654/majority-element-2-easy-dictionary
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: h={} res=[] for i in nums: if i not in h: h[i]=1 else: h[i]+=1 for key,value in h.items(): if value>len(nums)//3: res.append(key) return res
majority-element-ii
majority element 2 easy dictionary
anil5829354
0
24
majority element ii
229
0.442
Medium
4,228
https://leetcode.com/problems/majority-element-ii/discuss/1993541/120-ms-Super-Easy-and-Fast-Solution-in-Python-using-dictionary-T.C-greaterO(n)
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: d={} for i in nums: if i in d: d[i]+=1 else: d[i]=1 len_nums=len(nums) m=max(d.values()) return (i for i,v in d.items() if(v>len_nums/3))
majority-element-ii
120 ms Super Easy and Fast Solution in Python using dictionary T.C->O(n)
karansinghsnp
0
53
majority element ii
229
0.442
Medium
4,229
https://leetcode.com/problems/majority-element-ii/discuss/1846816/Python-easy-to-read-and-understand-or-hashmap
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: d = {} n = len(nums) for num in nums: d[num] = d.get(num, 0) + 1 ans = [] for key in d: if d[key] > n//3: ans.append(key) return ans
majority-element-ii
Python easy to read and understand | hashmap
sanial2001
0
41
majority element ii
229
0.442
Medium
4,230
https://leetcode.com/problems/majority-element-ii/discuss/1789701/Python-Easy-One-Liner-No-imports
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: return [num for num in set(nums) if nums.count(num) > (len(nums)/3)]
majority-element-ii
Python Easy One Liner - No imports
Rcoff
0
76
majority element ii
229
0.442
Medium
4,231
https://leetcode.com/problems/majority-element-ii/discuss/1789701/Python-Easy-One-Liner-No-imports
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: output_list = [] for num in set(nums): if nums.count(num) > (len(nums) / 3): output_list.append(num) return output_list
majority-element-ii
Python Easy One Liner - No imports
Rcoff
0
76
majority element ii
229
0.442
Medium
4,232
https://leetcode.com/problems/majority-element-ii/discuss/1789342/Python3-Hashmap-Solution
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: c = Counter(nums) l1 = [] for i in c: if c[i] > len(nums)/3: l1.append(i) return l1
majority-element-ii
Python3 Hashmap Solution
rakin54
0
36
majority element ii
229
0.442
Medium
4,233
https://leetcode.com/problems/majority-element-ii/discuss/1787634/Python-two-Lines...Solution
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: d={i:nums.count(i) for i in set(nums)} return [i for i in d.keys() if d[i]>len(nums)//3]
majority-element-ii
Python two Lines...Solution
AjayKadiri
0
42
majority element ii
229
0.442
Medium
4,234
https://leetcode.com/problems/majority-element-ii/discuss/1728846/Python-74-faster-93-less-memory-using-dictionary
class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: List[int] """ d={} for n in nums: d[n] = d.get(n,0)+1 return [k for k in d if d.get(k)>len(nums)//3]
majority-element-ii
Python 74% faster, 93% less memory using dictionary
Hacksaw203
0
68
majority element ii
229
0.442
Medium
4,235
https://leetcode.com/problems/majority-element-ii/discuss/1727643/Python-easy-solution-using-for-loops-and-hashmap
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: seen = {} outarr = [] for i in range(0,len(nums)): if nums[i] not in seen: seen[nums[i]] = 1 else: seen[nums[i]] += 1 for key, value in seen.items(): if value > len(nums)//3: outarr.append(key) return outarr
majority-element-ii
Python easy solution using for loops and hashmap
x7z
0
40
majority element ii
229
0.442
Medium
4,236
https://leetcode.com/problems/majority-element-ii/discuss/1639279/Python-oror-Boyer-Moore-Voting-Algo-oror-O(n)-time-O(1)-space-complexity
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: #at max two majority elem possible elem1 = elem2 =None freq1 = freq2 = 0 for val in nums: if val == elem1: freq1 += 1 elif val == elem2: freq2 += 1 elif freq1 == 0: elem1 = val freq1 = 1 elif freq2 == 0: elem2 = val freq2 = 1 else: freq1 -= 1 freq2 -= 1 #at this point, we have two elements with maximum frequency #now we need to check the frequency of these two elements, if the particular elements appear more than n//3 times count1 = count2 = 0 for val in nums: if val == elem1: count1 += 1 elif val == elem2: count2 += 1 res = [] if count1 > len(nums) // 3: res.append(elem1) if count2 > len(nums) // 3: res.append(elem2) return res
majority-element-ii
Python || Boyer-Moore Voting Algo || O(n) time O(1) space complexity
s_m_d_29
0
153
majority element ii
229
0.442
Medium
4,237
https://leetcode.com/problems/majority-element-ii/discuss/1614316/faster-than-96.23-python-code
class Solution: def majorityElement(self, nums: List[int]) -> int: min=len(nums)/3 op=[] s=set(nums) for num in s: if(nums.count(num)>min): op.append(num) return op
majority-element-ii
faster than 96.23% python code
Agdayma02
0
85
majority element ii
229
0.442
Medium
4,238
https://leetcode.com/problems/majority-element-ii/discuss/1495990/Python3-Two-kind-of-solutions
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: d = {} for num in nums: if num not in d: d[num] = 0 d[num] += 1 res = [] k = len(nums) // 3 for key in d: if d[key] > k: res.append(key) return res # Boyer-Moore Vote class Solution: def majorityElement(self, nums: List[int]) -> List[int]: count1 = count2 = 0 candidate1, candidate2 = 0, 1 # testcase: [0, 0, 0] for num in nums: if num == candidate1: count1 += 1 elif num == candidate2: count2 += 1 elif count1 == 0: candidate1, count1 = num, 1 elif count2 == 0: candidate2, count2 = num, 2 else: count1 -= 1 count2 -= 1 res = [] for candidate in [candidate1, candidate2]: cnt = 0 for num in nums: if num == candidate: cnt += 1 if cnt > len(nums) // 3: res.append(candidate) return res
majority-element-ii
[Python3] Two kind of solutions
maosipov11
0
110
majority element ii
229
0.442
Medium
4,239
https://leetcode.com/problems/majority-element-ii/discuss/1411168/One-line-using-Python-Counter-faster-than-94.62
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: l = len(nums)/3 return [i for i, j in Counter(nums).items() if j > l]
majority-element-ii
One line using Python Counter, faster than 94.62%
vagmithra
0
94
majority element ii
229
0.442
Medium
4,240
https://leetcode.com/problems/majority-element-ii/discuss/1203256/Simple-python3-solution
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: d = {} times = len(nums)//3 arr = [] for i in nums: d[i] = d[i]+1 if i in d else 1 for i in d: if d[i] > times: arr.append(i) return arr
majority-element-ii
Simple python3 solution
abhijeetmallick29
0
58
majority element ii
229
0.442
Medium
4,241
https://leetcode.com/problems/majority-element-ii/discuss/859304/Python3-boyer-moore
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: ans, vote = [None]*2, [0]*2 for x in nums: if vote[0] == 0 and x not in ans: ans[0] = x elif vote[1] == 0 and x not in ans: ans[1] = x if ans[0] == x: vote[0] += 1 elif ans[1] == x: vote[1] += 1 else: vote = [x-1 for x in vote] return [x for x in ans if nums.count(x) > len(nums)//3]
majority-element-ii
[Python3] boyer-moore
ye15
0
73
majority element ii
229
0.442
Medium
4,242
https://leetcode.com/problems/majority-element-ii/discuss/859304/Python3-boyer-moore
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: n = 2 ans, vote = [None]*n, [0]*n for x in nums: for i in range(n): if vote[i] == 0 and x not in ans: ans[i] = x break for i in range(n): if ans[i] == x: vote[i] += 1 break else: vote = [x-1 for x in vote] return [x for x in ans if nums.count(x) > len(nums)//(n+1)]
majority-element-ii
[Python3] boyer-moore
ye15
0
73
majority element ii
229
0.442
Medium
4,243
https://leetcode.com/problems/majority-element-ii/discuss/653594/Python-99.90-speed..-O(N)-space-though
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: temp = set(nums) output = [] k = len(nums) for i in temp: if nums.count(i) > (k//3) and i not in output: output.append(i) return output
majority-element-ii
Python 99.90% speed.., O(N) space though
Chris_R
0
184
majority element ii
229
0.442
Medium
4,244
https://leetcode.com/problems/majority-element-ii/discuss/505545/Python3-super-simple-5-lines-code
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: n,ht,res=len(nums),collections.defaultdict(int),set() for num in nums: ht[num]+=1 if ht[num]>n/3: res.add(num) return list(res)
majority-element-ii
Python3 super simple 5-lines code
jb07
0
155
majority element ii
229
0.442
Medium
4,245
https://leetcode.com/problems/majority-element-ii/discuss/404244/Easy-Python-Solution-beats-95
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: d = [] if nums==[]: return if len(nums)==1: return nums if len(nums)<3: return list(set(nums)) for x in set(nums): t = nums.count(x) if (t>len(nums)//3): d.append(x) return d
majority-element-ii
Easy Python Solution beats 95%
saffi
-1
587
majority element ii
229
0.442
Medium
4,246
https://leetcode.com/problems/majority-element-ii/discuss/1235542/Simpliest-Solution-in-python-Beats-98.66
class Solution: def majorityElement(self, nums: List[int]) -> List[int]: return [v for v in set(nums) if nums.count(v)>math.floor(len(nums)/3)]
majority-element-ii
Simpliest Solution in python Beats 98.66 %
dhrumilg699
-4
169
majority element ii
229
0.442
Medium
4,247
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/1960632/Inorder-%2B-Heap-In-Python
class Solution: import heapq def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: heap = [] def inorder(r): if r: inorder(r.left) heapq.heappush(heap,-(r.val)) if len(heap) > k: heapq.heappop(heap) inorder(r.right) inorder(root) return -heapq.heappop(heap)
kth-smallest-element-in-a-bst
Inorder + Heap In Python
gamitejpratapsingh998
2
83
kth smallest element in a bst
230
0.695
Medium
4,248
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/2291420/Python-Recursive-Simple
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: res = [] def bst(node): if (not node or (len(res) >= k)): return bst(node.left) res.append(node.val) bst(node.right) bst(root) return res[k - 1]
kth-smallest-element-in-a-bst
Python Recursive Simple
soumyadexter7
1
68
kth smallest element in a bst
230
0.695
Medium
4,249
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/1963983/easy-to-understand-python-approach
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: l=[] def traverse(node): if not node: return l.append(node.val) traverse(node.left) traverse(node.right) traverse(root) l.sort() l=list(set(l)) return l[k-1]
kth-smallest-element-in-a-bst
easy to understand python approach
captain_sathvik
1
52
kth smallest element in a bst
230
0.695
Medium
4,250
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/1911825/python3-inorder-without-a-List
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: count = 0 ans = 0 def inorder(node: Optional[TreeNode]) -> None: nonlocal count, ans if not node: return inorder(node.left) count += 1 if count == k: ans = node.val return inorder(node.right) inorder(root) return ans
kth-smallest-element-in-a-bst
python3 inorder without a List
parmenio
1
41
kth smallest element in a bst
230
0.695
Medium
4,251
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/1555966/Python-or-faster-than-90-and-less-than-98-or-simple-dfs-solution
class Solution: def dfs(self, root: TreeNode, result:list) -> None: if root: self.dfs(root.left, result) result.append(root.val) self.dfs(root.right, result) def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: if root: result = [] self.dfs(root, result) return result[k-1]
kth-smallest-element-in-a-bst
Python | faster than 90% and less than 98% | simple dfs solution
He11oWor1d
1
253
kth smallest element in a bst
230
0.695
Medium
4,252
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/1348183/Easy-Fast-Memory-Efficient-Recursive-Python-Solution-(faster-than-93-Memory-less-than-96)
class Solution: def __init__(self): self.node_values = [] self.k = 0 def dfs(self, root): # inorder traversal if root: self.dfs(root.left) # if length of node_values becomes k, we found our element if len(self.node_values) == self.k: return self.node_values.append(root.val) self.dfs(root.right) def kthSmallest(self, root: TreeNode, k: int) -> int: self.k = k self.dfs(root) return self.node_values[-1]
kth-smallest-element-in-a-bst
Easy, Fast, Memory Efficient Recursive Python Solution (faster than 93%, Memory less than 96%)
the_sky_high
1
191
kth smallest element in a bst
230
0.695
Medium
4,253
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/2825530/Python-solution
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: heap = [] def BrowseTree(node): if node: heapq.heappush(heap, node.val) if node.left: BrowseTree(node.left) if node.right: BrowseTree(node.right) BrowseTree(root) return heapq.nsmallest(k,heap)[-1]
kth-smallest-element-in-a-bst
Python solution
maomao1010
0
1
kth smallest element in a bst
230
0.695
Medium
4,254
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/2815608/Python-DFS-inorder-traversal-with-explanation
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: res = [] def dfs(root): # dfs inorder traversal of a BST will be in ascending order if not root.left and not root.right: # if it's a leaf node, append to res and return, as it's the base case # here we also needs to handle edge case of 1 node tree res.append(root.val) return res[0] if k == 1 else None if root.left: # go leftmost as we can first dfs(root.left) # then middle (root) res.append(root.val) if root.right: # then right dfs(root.right) if len(res) >= k: # after each stack of recursion is done, # we check if our res list already contains the kth smallest element return res[k-1] return dfs(root)
kth-smallest-element-in-a-bst
Python DFS inorder traversal with explanation
deezeey
0
1
kth smallest element in a bst
230
0.695
Medium
4,255
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/2732197/Modified-Inorder-TC%3A-O(log(n))-SC%3A-O(1)
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: ans = 0 cnt = 0 def inorder(root): if not root: return None inorder(root.left) nonlocal cnt, ans cnt += 1 if cnt == k: ans = root.val return inorder(root.right) inorder(root) return ans
kth-smallest-element-in-a-bst
Modified Inorder TC: O(log(n)) SC: O(1)
hacktheirlives
0
4
kth smallest element in a bst
230
0.695
Medium
4,256
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/2724418/DFS-Solution-or-Easy-to-Understand-or-Python
class Solution(object): def kthSmallest(self, root, k): nums = [] def dfs(root, k): if not root: return dfs(root.left, k) nums.append(root.val) dfs(root.right, k) dfs(root, k) return nums[k-1]
kth-smallest-element-in-a-bst
DFS Solution | Easy to Understand | Python
its_krish_here
0
8
kth smallest element in a bst
230
0.695
Medium
4,257
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/2716592/Inorder-way-Python-and-Golang-O(N)
class Solution: def inorder_generator(self, root: Optional[TreeNode]) -> Generator: if root: yield from self.inorder_generator(root.left) yield root.val yield from self.inorder_generator(root.right) def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: for i, val in enumerate(self.inorder_generator(root), 1): if i == k: return val
kth-smallest-element-in-a-bst
Inorder way [Python and Golang] O(N)
namashin
0
9
kth smallest element in a bst
230
0.695
Medium
4,258
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/2698218/python3-iterative-Sol.-easy-and-clean-solution
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: stack=[] while root or stack: while root: stack.append(root) root=root.left root=stack.pop() k-=1 if k==0: return root.val root=root.right
kth-smallest-element-in-a-bst
python3 iterative Sol. easy and clean solution
pranjalmishra334
0
5
kth smallest element in a bst
230
0.695
Medium
4,259
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/2600434/Python-Solution-or-Recursion-or-Inorder
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: def helper(root): elements=[] if root is None: return elements elements.append(root.val) if root.left: elements+=helper(root.left) if root.right: elements+=helper(root.right) return elements ans=helper(root) ans.sort() return ans[k-1]
kth-smallest-element-in-a-bst
Python Solution | Recursion | Inorder
Siddharth_singh
0
42
kth smallest element in a bst
230
0.695
Medium
4,260
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/2533903/Python-BFS-Beats-97-oror-DFS-Beats-86-Solutions
class Solution: # Time: O(n) and Space: O(n) def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: stack = [] # We are performing inorder traversal(sorted traversal) i.e. the first node to be appended in the stack will be # the smallest one, so we will decrease the k's value to when appending in stack, # when k = 0 meaning the appended node is the kth smallest element while root or stack: # till either the tree or the stack is not empty while root: stack.append(root) root = root.left # after reaching the leftest node, pop it and traverse its right child as it does not have any left child that's why the while root stopped root = stack.pop() k -= 1 # decreasing k's value as it will indicate the popped element is kth smallest or not if k == 0: return root.val root = root.right
kth-smallest-element-in-a-bst
Python [ BFS - Beats 97% || DFS - Beats 86% ] Solutions
DanishKhanbx
0
82
kth smallest element in a bst
230
0.695
Medium
4,261
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/2533903/Python-BFS-Beats-97-oror-DFS-Beats-86-Solutions
class Solution: # Time: O(n) and Space: O(n) def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: self.k = k self.res = None self.helper(root) return self.res def helper(self, node): if not node: return self.helper(node.left) self.k -= 1 if self.k == 0: self.res = node.val return self.helper(node.right)
kth-smallest-element-in-a-bst
Python [ BFS - Beats 97% || DFS - Beats 86% ] Solutions
DanishKhanbx
0
82
kth smallest element in a bst
230
0.695
Medium
4,262
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/2339475/Easiest-python-solution-just-dfs
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: # Simple dfs solution # visit all the node and append all the node in list # Then sort the list , and return desired element's index. if not root: return res=[] self.dfs(root,res) return sorted(res)[k-1] def dfs(self,root,res): if not root: return res.append(root.val) if root.left: self.dfs(root.left,res) if root.right: self.dfs(root.right,res)
kth-smallest-element-in-a-bst
Easiest python solution , just dfs
Aniket_liar07
0
80
kth smallest element in a bst
230
0.695
Medium
4,263
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/2314547/Python-DFS-In-order-traversal
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: self.res = None self.count = 1 def dfs(root): if not root: return if self.res: return dfs(root.left) if self.res is None and self.count == k: self.res = root.val return self.count += 1 dfs(root.right) dfs(root) return self.res
kth-smallest-element-in-a-bst
Python DFS, In-order traversal
Strafespey
0
75
kth smallest element in a bst
230
0.695
Medium
4,264
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/2015732/Python-Recursive-Solution
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: ans, count = 0, 0 def dfs(root): nonlocal ans, count if root is None: return dfs(root.left) count += 1 if count == k: ans = root.val dfs(root.right) dfs(root) return ans
kth-smallest-element-in-a-bst
Python Recursive Solution
user6397p
0
38
kth smallest element in a bst
230
0.695
Medium
4,265
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/1962492/Python3-wexplanation
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: tree = [] self.inorder(root, tree) return tree[k-1] def inorder(self, root: Optional[TreeNode], l: List[int]): if root == None: return self.inorder(root.left, l) l.append(root.val) self.inorder(root.right, l) ```
kth-smallest-element-in-a-bst
Python3 w/explanation
rohkal
0
17
kth smallest element in a bst
230
0.695
Medium
4,266
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/1961460/Pythonor-Simple-Inorder-Traversal-Solution
class Solution: def inorder(self,root,lis): if(root==None): return self.inorder(root.left,lis) lis.append(root.val) self.inorder(root.right,lis) def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: lis=[] self.inorder(root,lis) return lis[k-1]
kth-smallest-element-in-a-bst
Python| Simple Inorder Traversal Solution
backpropagator
0
27
kth smallest element in a bst
230
0.695
Medium
4,267
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/1961000/Python-Solution-or-Two-Ways-or-Over-90-Faster-or-Inorder-Traversal-Trick
class Solution: def __init__(self): self.count = None self.ans = None def inorder(self,root): if root is None: return None self.inorder(root.left) self.count -= 1 if self.count == 0: self.ans = root.val self.inorder(root.right) def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: self.count = k self.inorder(root) return self.ans
kth-smallest-element-in-a-bst
Python Solution | Two Ways | Over 90% Faster | Inorder Traversal Trick
Gautam_ProMax
0
27
kth smallest element in a bst
230
0.695
Medium
4,268
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/1960321/2-pythonic-solutions%3A-inorder_traversal-and-heap-(faster-than-91)
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: def inorder_traverse(root, stack): if not root or len(stack) >= k: return inorder_traverse(root.left, stack) stack.append(root.val) inorder_traverse(root.right, stack) # Traverse the tree to store the node values into a list. Time: O(n) Space: O(n) stack = [] inorder_traverse(root, stack) return stack[k-1] # since k is 1-indexed we substract 1
kth-smallest-element-in-a-bst
2 pythonic solutions: inorder_traversal and heap (faster than 91%)
chibby0ne
0
19
kth smallest element in a bst
230
0.695
Medium
4,269
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/1960202/Python3-Solution-with-using-recursive-dfs
class Solution: def __init__(self): self.res = -1 self.k = 0 def traversal(self, node, is_reached): if not node or self.k == 0: return True is_finished = self.traversal(node.left, is_reached) if is_finished or is_reached: self.k -= 1 if self.k == 0: self.res = node.val self.traversal(node.right, is_reached or is_finished) return is_reached or is_finished def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: self.k = k self.traversal(root, False) return self.res
kth-smallest-element-in-a-bst
[Python3] Solution with using recursive dfs
maosipov11
0
3
kth smallest element in a bst
230
0.695
Medium
4,270
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/1959951/Python-inorder-solution
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: val = [] def inorder(root): if not root: return inorder(root.left) val.append(root.val) inorder(root.right) inorder(root) return val[k-1]
kth-smallest-element-in-a-bst
Python inorder solution
alessiogatto
0
49
kth smallest element in a bst
230
0.695
Medium
4,271
https://leetcode.com/problems/kth-smallest-element-in-a-bst/discuss/1959830/Python-Solution-Inorder-Traversal-79-faster
class Solution: def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: node_vals = [] def traverse_inorder(node): if node is None: return traverse_inorder(node.left) node_vals.append(node.val) traverse_inorder(node.right) traverse_inorder(root) return node_vals[k - 1]
kth-smallest-element-in-a-bst
Python Solution, Inorder Traversal, 79% faster
pradeep288
0
9
kth smallest element in a bst
230
0.695
Medium
4,272
https://leetcode.com/problems/power-of-two/discuss/948641/Python-O(1)-Solution
class Solution: def isPowerOfTwo(self, n: int) -> bool: return n>0 and n&amp;(n-1)==0
power-of-two
Python O(1) Solution
lokeshsenthilkumar
37
2,400
power of two
231
0.457
Easy
4,273
https://leetcode.com/problems/power-of-two/discuss/948641/Python-O(1)-Solution
class Solution: def isPowerOfTwo(self, n: int) -> bool: return n>0 and sum(list(map(int,bin(n)[2:])))==1
power-of-two
Python O(1) Solution
lokeshsenthilkumar
37
2,400
power of two
231
0.457
Easy
4,274
https://leetcode.com/problems/power-of-two/discuss/2375156/Very-Easy-0-ms-100-(Fully-Explained)(Java-C%2B%2B-Python-JS-C-Python3)
class Solution(object): def isPowerOfTwo(self, n): # If n <= 0 that means its a negative hence not a power of 2... if n <= 0: return False if n == 1: return True # Keep dividing the number by β€˜2’ until it is not divisible by β€˜2’ anymore. while (n % 2 == 0): n /= 2 # If n is equal to 1, The integer is a power of two otherwise false... return n == 1
power-of-two
Very Easy 0 ms 100% (Fully Explained)(Java, C++, Python, JS, C, Python3)
PratikSen07
22
1,200
power of two
231
0.457
Easy
4,275
https://leetcode.com/problems/power-of-two/discuss/2375156/Very-Easy-0-ms-100-(Fully-Explained)(Java-C%2B%2B-Python-JS-C-Python3)
class Solution: def isPowerOfTwo(self, n: int) -> bool: # If n <= 0 that means its a negative hence not a power of 2... if n <= 0: return False if n == 1: return True # Keep dividing the number by β€˜2’ until it is not divisible by β€˜2’ anymore. while (n % 2 == 0): n /= 2 # If n is equal to 1, The integer is a power of two otherwise false... return n == 1
power-of-two
Very Easy 0 ms 100% (Fully Explained)(Java, C++, Python, JS, C, Python3)
PratikSen07
22
1,200
power of two
231
0.457
Easy
4,276
https://leetcode.com/problems/power-of-two/discuss/1338827/Simple-Python-Solution-for-%22Power-of-Two%22
class Solution: def isPowerOfTwo(self, n: int) -> bool: if (n == 0): return False while (n != 1): if (n % 2 != 0): return False n = n // 2 return True
power-of-two
Simple Python Solution for "Power of Two"
sakshikhandare2527
6
793
power of two
231
0.457
Easy
4,277
https://leetcode.com/problems/power-of-two/discuss/1237879/WEEB-DOES-PYTHON-(ONE-LINER)
class Solution: def isPowerOfTwo(self, n: int) -> bool: return n>0 and log2(n) == int(log2(n))
power-of-two
WEEB DOES PYTHON (ONE-LINER)
Skywalker5423
5
226
power of two
231
0.457
Easy
4,278
https://leetcode.com/problems/power-of-two/discuss/382662/Python-solutions
class Solution: def isPowerOfTwo(self, n: int) -> bool: return n > 0 and (n &amp; (n - 1)) == 0
power-of-two
Python solutions
amchoukir
3
534
power of two
231
0.457
Easy
4,279
https://leetcode.com/problems/power-of-two/discuss/382662/Python-solutions
class Solution: def isPowerOfTwo(self, n: int) -> bool: if n <= 0: return False while n > 1: if n &amp; 1: return False n //= 2 return True
power-of-two
Python solutions
amchoukir
3
534
power of two
231
0.457
Easy
4,280
https://leetcode.com/problems/power-of-two/discuss/382662/Python-solutions
class Solution: def isPowerOfTwo(self, n: int) -> bool: if n <= 0: return False power = 1 while n >= power: if n == power: return True power *= 2 return False
power-of-two
Python solutions
amchoukir
3
534
power of two
231
0.457
Easy
4,281
https://leetcode.com/problems/power-of-two/discuss/382662/Python-solutions
class Solution: def isPowerOfTwo(self, n: int) -> bool: return n > 0 and (1073741824%n == 0)
power-of-two
Python solutions
amchoukir
3
534
power of two
231
0.457
Easy
4,282
https://leetcode.com/problems/power-of-two/discuss/382662/Python-solutions
class Solution: def isPowerOfTwo(self, n: int) -> bool: return n > 0 and sum(1 for c in bin(n)[2:] if c == '1') == 1
power-of-two
Python solutions
amchoukir
3
534
power of two
231
0.457
Easy
4,283
https://leetcode.com/problems/power-of-two/discuss/382662/Python-solutions
class Solution: def isPowerOfTwo(self, n: int) -> bool: return n > 0 and bin(n).count('1') == 1
power-of-two
Python solutions
amchoukir
3
534
power of two
231
0.457
Easy
4,284
https://leetcode.com/problems/power-of-two/discuss/1638643/Python3-DIVIDE-AND-CONQUER-(**)-Explained
class Solution: def isPowerOfTwo(self, n: int) -> bool: if n < 1: return False while not n % 2: n = n/2 return n == 1
power-of-two
βœ”οΈ [Python3] DIVIDE AND CONQUER (‒̀ᴗ‒́)و Μ‘Μ‘, Explained
artod
2
292
power of two
231
0.457
Easy
4,285
https://leetcode.com/problems/power-of-two/discuss/1293868/Python3-one-liner-without-for-cycle-explained
class Solution: def isPowerOfTwo(self, n: int) -> bool: return str(bin(n))[2:].count("1") == 1 and n > 0
power-of-two
Python3, one liner, without for cycle, explained
albezx0
2
135
power of two
231
0.457
Easy
4,286
https://leetcode.com/problems/power-of-two/discuss/1117921/Python-3-different-locally-optimal-approaches!
class Solution: def isPowerOfTwo(self, n: int) -> bool: epsilon = 0.0000000001 if not n > 0: return False logged = (math.log(abs(n), 2))%1 if (logged < epsilon or logged > 1 - epsilon): return True
power-of-two
[Python] 3 different locally optimal approaches!
larskl
2
169
power of two
231
0.457
Easy
4,287
https://leetcode.com/problems/power-of-two/discuss/1117921/Python-3-different-locally-optimal-approaches!
class Solution: def isPowerOfTwo(self, n: int) -> bool: while n%2==0 and n>=8: n = n/8 while n%2==0 and n>=2: n = n/2 if n == 1: return True
power-of-two
[Python] 3 different locally optimal approaches!
larskl
2
169
power of two
231
0.457
Easy
4,288
https://leetcode.com/problems/power-of-two/discuss/1117921/Python-3-different-locally-optimal-approaches!
class Solution: def isPowerOfTwo(self, n: int) -> bool: return n > 0 and bin(n).count('1') == 1
power-of-two
[Python] 3 different locally optimal approaches!
larskl
2
169
power of two
231
0.457
Easy
4,289
https://leetcode.com/problems/power-of-two/discuss/676734/python-o1-solution
class Solution: def isPowerOfTwo(self, n: int) -> bool: origin = 1 while n > origin: origin = origin * 2 if n == origin: return True return False
power-of-two
python o1 solution
yingziqing123
2
535
power of two
231
0.457
Easy
4,290
https://leetcode.com/problems/power-of-two/discuss/1794107/Python3-or-3-solution-or-bit-manipulation-or-while-loop
class Solution: def isPowerOfTwo(self, n: int) -> bool: return (n!=0) and (n&amp;(n-1)==0)
power-of-two
Python3 | 3 solution | bit manipulation | while loop
Anilchouhan181
1
86
power of two
231
0.457
Easy
4,291
https://leetcode.com/problems/power-of-two/discuss/1794107/Python3-or-3-solution-or-bit-manipulation-or-while-loop
class Solution: def isPowerOfTwo(self, n: int) -> bool: return n > 0 and (n &amp; (n - 1)) == 0
power-of-two
Python3 | 3 solution | bit manipulation | while loop
Anilchouhan181
1
86
power of two
231
0.457
Easy
4,292
https://leetcode.com/problems/power-of-two/discuss/1794107/Python3-or-3-solution-or-bit-manipulation-or-while-loop
class Solution: def isPowerOfTwo(self, n: int) -> bool: while n!=1: if n<1: return False n=n/2 return True
power-of-two
Python3 | 3 solution | bit manipulation | while loop
Anilchouhan181
1
86
power of two
231
0.457
Easy
4,293
https://leetcode.com/problems/power-of-two/discuss/1791077/Python-3-(40ms)-or-BIT-AND-Formula-Solution-or-Easy-to-Understand
class Solution: def isPowerOfTwo(self, n: int) -> bool: if n > 0 and (n &amp; (n - 1)) == 0: return True return False
power-of-two
Python 3 (40ms) | BIT AND Formula Solution | Easy to Understand
MrShobhit
1
101
power of two
231
0.457
Easy
4,294
https://leetcode.com/problems/power-of-two/discuss/1233645/Python-or-Time-Complexity-O(1)-or-Better-than-96-or-Simple-and-Clean
class Solution: def isPowerOfTwo(self, n: int) -> bool: # If n is negative or 0 if n<=0: return False # If the highest possible power of 2 is perfectly divisible by n if 2**63%n==0: return True return False
power-of-two
Python | Time Complexity O(1) | Better than 96% | Simple & Clean
shuklaeshita0209
1
114
power of two
231
0.457
Easy
4,295
https://leetcode.com/problems/power-of-two/discuss/805003/Python3-solution-with-recursion
class Solution: def isPowerOfTwo(self, n: int) -> bool: if n <= 0: return False if n == 1: return True if n % 2 > 0: return False return self.isPowerOfTwo(n//2)
power-of-two
Python3 solution with recursion
user6758m
1
105
power of two
231
0.457
Easy
4,296
https://leetcode.com/problems/power-of-two/discuss/418754/Python-Easy-to-understand-solution
class Solution: def isPowerOfTwo(self, n: int) -> bool: if bin(n)[2:].count('1') > 1 or n <= 0: return False return True
power-of-two
Python Easy to understand solution
bhanga
1
180
power of two
231
0.457
Easy
4,297
https://leetcode.com/problems/power-of-two/discuss/2845440/python-recursice
class Solution: def isPowerOfTwo(self, n: int) -> bool: if n==0: return False if n==1 or n==2: return True if n%2 : return False return self.isPowerOfTwo(n/2)
power-of-two
python recursice
Cosmodude
0
1
power of two
231
0.457
Easy
4,298
https://leetcode.com/problems/power-of-two/discuss/2842279/Python3Faster-than-84.90-of-Python3-online-submissions
class Solution: def isPowerOfTwo(self, n: int) -> bool: i = 0 while 2**i <= n: if 2**i == n: return True i += 1 return False
power-of-two
Python3[Faster than 84.90% of Python3 online submissions]
Silvanus20
0
2
power of two
231
0.457
Easy
4,299