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/add-digits/discuss/948670/Python-one-liner | class Solution:
def addDigits(self, num: int) -> int:
return 1+(num-1)%9 | add-digits | Python one-liner | lokeshsenthilkumar | 7 | 546 | add digits | 258 | 0.635 | Easy | 4,700 |
https://leetcode.com/problems/add-digits/discuss/1930593/Python-Math-Solution-Nice-Explanation-With-Examples-Try-it | class Solution:
def addDigits(self, num: int) -> int:
if num == 0:
return 0
return num % 9 if num % 9 != 0 else 9 | add-digits | Python Math Solution Nice Explanation With Examples, Try it | Hejita | 4 | 73 | add digits | 258 | 0.635 | Easy | 4,701 |
https://leetcode.com/problems/add-digits/discuss/2765562/Python3ororO(1) | class Solution:
def addDigits(self, num: int) -> int:
if ( num == 0 ): return 0
if num%9 == 0:
return 9
else:
return num%9 | add-digits | Python3||O(1) | Sneh713 | 2 | 97 | add digits | 258 | 0.635 | Easy | 4,702 |
https://leetcode.com/problems/add-digits/discuss/590532/easy-python-solution-beats-95-runtime-using-divmod | class Solution:
def addDigits(self, num: int) -> int:
while num>9:
d,m=divmod(num,10)
num=d+m
return num | add-digits | easy python π solution beats 95% runtime using divmod | InjySarhan | 2 | 234 | add digits | 258 | 0.635 | Easy | 4,703 |
https://leetcode.com/problems/add-digits/discuss/2436041/python-8-liner-oror-73.6-MS-Faster-easy-solution | class Solution:
def addDigits(self, num: int) -> int:
while len(str(num))!=1:
SumOfDigits=0
for i in str(num):
SumOfDigits=SumOfDigits+int(i)
num=SumOfDigits
SumOfDigits=0
return num | add-digits | python 8 liner || 73.6 MS Faster easy solution | keertika27 | 1 | 42 | add digits | 258 | 0.635 | Easy | 4,704 |
https://leetcode.com/problems/add-digits/discuss/1754063/memory-beats-99-simple-python-answer | class Solution:
def addDigits(self, num: int) -> int:
if num == 0:
return 0
a = num%9
if a == 0:
return 9
return a | add-digits | memory beats 99 % simple python answer | ggeeoorrggee | 1 | 26 | add digits | 258 | 0.635 | Easy | 4,705 |
https://leetcode.com/problems/add-digits/discuss/1234347/Simple-and-beginner-friendly-solution | class Solution:
def addDigits(self, num: int) -> int:
# general approach
sum = 0
while(num > 0 or sum > 9):
if(num == 0):
num = sum
sum = 0
sum += num % 10
num = num // 10
return sum | add-digits | Simple and beginner friendly solution | nandanabhishek | 1 | 92 | add digits | 258 | 0.635 | Easy | 4,706 |
https://leetcode.com/problems/add-digits/discuss/1158996/Python3-Simple-Recursion-Approach | class Solution:
def addDigits(self, num: int) -> int:
def dp(n: str):
if(len(n) == 1): return n
return dp(str(sum(map(int , list(n)))))
return int(dp(str(num))) | add-digits | [Python3] Simple Recursion Approach | Lolopola | 1 | 76 | add digits | 258 | 0.635 | Easy | 4,707 |
https://leetcode.com/problems/add-digits/discuss/1142432/Python-96.54-faster | class Solution:
def addDigits(self, num: int) -> int:
i = sum([int(x) for x in str(num)])
while i > 9:
i = sum([int(x) for x in str(i)])
return i | add-digits | Python 96.54% faster | Valarane | 1 | 90 | add digits | 258 | 0.635 | Easy | 4,708 |
https://leetcode.com/problems/add-digits/discuss/757891/Python3-math-and-simulation | class Solution:
def addDigits(self, num: int) -> int:
return num and 1 + (num - 1) % 9 | add-digits | [Python3] math & simulation | ye15 | 1 | 31 | add digits | 258 | 0.635 | Easy | 4,709 |
https://leetcode.com/problems/add-digits/discuss/757891/Python3-math-and-simulation | class Solution:
def addDigits(self, num: int) -> int:
while num >= 10:
num = sum(int(x) for x in str(num))
return num | add-digits | [Python3] math & simulation | ye15 | 1 | 31 | add digits | 258 | 0.635 | Easy | 4,710 |
https://leetcode.com/problems/add-digits/discuss/336201/Solution-in-Python-3-(one-line) | class Solution:
def addDigits(self, num: int) -> int:
return 9 if num!= 0 and num%9 == 0 else num%9
- Python 3
- Junaid Mansuri | add-digits | Solution in Python 3 (one line) | junaidmansuri | 1 | 462 | add digits | 258 | 0.635 | Easy | 4,711 |
https://leetcode.com/problems/add-digits/discuss/2840384/python-one-liner | class Solution:
def addDigits(self, n: int) -> int:
# approach:
# if 0, return 0
# if n % 9 == 0, return 9
# else, return n % 9
return 0 if not n else 9 if not n % 9 else n % 9 | add-digits | python one-liner | wduf | 0 | 3 | add digits | 258 | 0.635 | Easy | 4,712 |
https://leetcode.com/problems/add-digits/discuss/2811184/Add-digits-challenge-using-Python-(please-help-me-upvote-it-if-u-feel-it-useful-thanks!) | class Solution:
def addDigits(self, num: int) -> int:
num = [int(i) for i in str(num)]
if len(num) == 1:
return num[0]
while(len(num) != 1):
sum = 0
for i in range(len(num)):
sum += num[i]
num = sum
num = [int(i) for i in str(num)]
return num[0] | add-digits | Add digits challenge using Python (please help me upvote it if u feel it useful, thanks!) | gokudera1111 | 0 | 4 | add digits | 258 | 0.635 | Easy | 4,713 |
https://leetcode.com/problems/add-digits/discuss/2774211/easy-code | class Solution:
def addDigits(self, num: int) -> int:
if(num<10):
return num
else:
while num>9:
num=num%10+num//10
return num | add-digits | easy code | sindhu_300 | 0 | 2 | add digits | 258 | 0.635 | Easy | 4,714 |
https://leetcode.com/problems/add-digits/discuss/2748424/Python3-Solution | class Solution:
def sumDigits(self, num: int) -> int:
total = 0
while num:
total += num%10
num //= 10
return total
def addDigits(self, num: int) -> int:
while num:
if num//10 == 0:
return num
num = self.sumDigits(num)
return 0 | add-digits | Python3 Solution | paul1202 | 0 | 2 | add digits | 258 | 0.635 | Easy | 4,715 |
https://leetcode.com/problems/add-digits/discuss/2748423/Python3-Solution | class Solution:
def sumDigits(self, num: int) -> int:
total = 0
while num:
total += num%10
num //= 10
return total
def addDigits(self, num: int) -> int:
while num:
if num//10 == 0:
return num
num = self.sumDigits(num)
return 0 | add-digits | Python3 Solution | paul1202 | 0 | 0 | add digits | 258 | 0.635 | Easy | 4,716 |
https://leetcode.com/problems/add-digits/discuss/2739329/Python3-Solution | class Solution:
def addDigits(self, num: int) -> int:
while num >= 10:
num = sum(int(i) for i in str(num))
return num | add-digits | Python3 Solution | dnvavinash | 0 | 2 | add digits | 258 | 0.635 | Easy | 4,717 |
https://leetcode.com/problems/add-digits/discuss/2731189/Python-(3-Steps) | class Solution:
def addDigits(self, num: int) -> int:
while(num>9):
sum=0
while(num):
rem=num%10
sum=sum+rem
num=num//10
num=sum
return num | add-digits | Python (3 Steps) | durgaraopolamarasetti | 0 | 2 | add digits | 258 | 0.635 | Easy | 4,718 |
https://leetcode.com/problems/add-digits/discuss/2695067/Recursive-solution | class Solution:
def addDigits(self, num: int) -> int:
def recur(x):
s = 0
while x > 0:
s += x%10
x //= 10
if s//10 > 0:
return recur(s)
return s
return recur(num) | add-digits | Recursive solution | MockingJay37 | 0 | 3 | add digits | 258 | 0.635 | Easy | 4,719 |
https://leetcode.com/problems/add-digits/discuss/2682239/Python-Solution | class Solution:
def addDigits(self, num: int) -> int:
listof =[0,1,2,3,4,5,6,7,8,9]
if num in listof:
return num
while len(str(num)) != 1:
num = str(num)
summ =0
for c in num:
summ = summ + int(c)
num = summ
return int(num) | add-digits | Python Solution | Sheeza | 0 | 6 | add digits | 258 | 0.635 | Easy | 4,720 |
https://leetcode.com/problems/add-digits/discuss/2668621/easy-python-solution | class Solution:
def addDigits(self, num: int) -> int:
while len(str(num))>1:
num = sum([int(x) for x in str(num)])
return num | add-digits | easy python solution | sahityasetu1996 | 0 | 5 | add digits | 258 | 0.635 | Easy | 4,721 |
https://leetcode.com/problems/add-digits/discuss/2620153/Python-3-Solution | class Solution:
def addDigits(self, num: int) -> int:
while num >9:
n = 0
for di in str(num):
n += int(di)
num = n
return num | add-digits | Python 3 Solution | sanzid | 0 | 9 | add digits | 258 | 0.635 | Easy | 4,722 |
https://leetcode.com/problems/add-digits/discuss/2556554/python-solution-or-space-complexity-O(1)or-easy-solution | class Solution:
def addDigits(self, num: int) -> int:
ans=num
while ans>9:
temp=0
while ans>0:
rem=ans%10
temp+=rem
ans=ans//10
ans=temp
return ans | add-digits | python solution | space complexity - O(1)| easy solution | I_am_SOURAV | 0 | 29 | add digits | 258 | 0.635 | Easy | 4,723 |
https://leetcode.com/problems/add-digits/discuss/2320792/easy-python-solution | class Solution:
def addDigits(self, num: int) -> int:
while len(str(num)) != 1 :
new_num = 0
for i in range(len(str(num))) :
new_num += int(str(num)[i])
num = new_num
return num | add-digits | easy python solution | sghorai | 0 | 48 | add digits | 258 | 0.635 | Easy | 4,724 |
https://leetcode.com/problems/add-digits/discuss/2256010/Easy-and-Simple-Recursion | class Solution:
def addDigits(self, num: int) -> int:
def ad(n):
o = 0
while n>0:
o += n%10
n = n//10
if o<10:
return o
if o>9:
return ad(o)
return ad(num) | add-digits | Easy and Simple Recursion | jayeshvarma | 0 | 36 | add digits | 258 | 0.635 | Easy | 4,725 |
https://leetcode.com/problems/add-digits/discuss/2176623/Simple-Python-solution | class Solution:
def addDigits(self, num: int) -> int:
num = list(str(num))
digit = 0
while True:
for i in num:
digit += int(i)
if digit < 10 and digit >= 0:
return digit
else:
num = list(str(digit))
digit = 0 | add-digits | Simple Python solution | ToastFreak | 0 | 41 | add digits | 258 | 0.635 | Easy | 4,726 |
https://leetcode.com/problems/add-digits/discuss/2176113/Simplest-Solution | class Solution:
def addDigits(self, num: int) -> int:
if len(str(num)) == 1:
return num
if num %9 == 0:
return 9
return num%9 | add-digits | Simplest Solution | Vaibhav7860 | 0 | 34 | add digits | 258 | 0.635 | Easy | 4,727 |
https://leetcode.com/problems/add-digits/discuss/2140751/Simple-Python-Better-than-93.29 | class Solution:
def addDigits(self, num: int) -> int:
if len(str(num))==1:
return num
while True:
l = [int(d) for d in str(num)]
num = sum(l)
if len(str(num))==1:
return num | add-digits | Simple Python, Better than 93.29% | suj2803 | 0 | 59 | add digits | 258 | 0.635 | Easy | 4,728 |
https://leetcode.com/problems/add-digits/discuss/2092782/WEEB-DOES-PYTHONC%2B%2B-(ONE-LINER) | class Solution:
def addDigits(self, num: int) -> int:
return num % 9 if (num % 9 != 0 or num == 0) else 9 | add-digits | WEEB DOES PYTHON/C++ (ONE-LINER) | Skywalker5423 | 0 | 33 | add digits | 258 | 0.635 | Easy | 4,729 |
https://leetcode.com/problems/add-digits/discuss/1947211/Python-easy-solution-oror-Runtime-35-ms | class Solution:
def addDigits(self, num: int) -> int:
ans=num;a=[ans]
while len(str(ans))!=1:
ans=str(ans)
b=sum(list(map(int,ans)))
if b not in a:
a.append(b);ans=b
else:
return num
return ans | add-digits | Python easy solution || Runtime 35 ms | Whitedevil07 | 0 | 39 | add digits | 258 | 0.635 | Easy | 4,730 |
https://leetcode.com/problems/add-digits/discuss/1882530/Python-Simple-and-Elegant!-Multiple-Solutions! | class Solution:
def addDigits(self, num):
while num >= 10:
q, r = num // 10, num % 10
num = q+r
return num | add-digits | Python - Simple and Elegant! Multiple Solutions! | domthedeveloper | 0 | 64 | add digits | 258 | 0.635 | Easy | 4,731 |
https://leetcode.com/problems/add-digits/discuss/1882530/Python-Simple-and-Elegant!-Multiple-Solutions! | class Solution:
def addDigits(self, num):
while num >= 10:
q, r = divmod(num, 10)
num = q+r
return num | add-digits | Python - Simple and Elegant! Multiple Solutions! | domthedeveloper | 0 | 64 | add digits | 258 | 0.635 | Easy | 4,732 |
https://leetcode.com/problems/add-digits/discuss/1882530/Python-Simple-and-Elegant!-Multiple-Solutions! | class Solution:
def addDigits(self, num):
while num >= 10: num = sum(divmod(num, 10))
return num | add-digits | Python - Simple and Elegant! Multiple Solutions! | domthedeveloper | 0 | 64 | add digits | 258 | 0.635 | Easy | 4,733 |
https://leetcode.com/problems/add-digits/discuss/1882530/Python-Simple-and-Elegant!-Multiple-Solutions! | class Solution:
def addDigits(self, num):
return num if num < 10 else self.addDigits(sum(divmod(num, 10))) | add-digits | Python - Simple and Elegant! Multiple Solutions! | domthedeveloper | 0 | 64 | add digits | 258 | 0.635 | Easy | 4,734 |
https://leetcode.com/problems/add-digits/discuss/1882530/Python-Simple-and-Elegant!-Multiple-Solutions! | class Solution:
def addDigits(self, num):
return num if num < 10 else self.addDigits(reduce(lambda x,y : int(x)+int(y), str(num))) | add-digits | Python - Simple and Elegant! Multiple Solutions! | domthedeveloper | 0 | 64 | add digits | 258 | 0.635 | Easy | 4,735 |
https://leetcode.com/problems/add-digits/discuss/1882530/Python-Simple-and-Elegant!-Multiple-Solutions! | class Solution:
def addDigits(self, num):
return num if num < 10 else self.addDigits(sum([int(i) for i in str(num)])) | add-digits | Python - Simple and Elegant! Multiple Solutions! | domthedeveloper | 0 | 64 | add digits | 258 | 0.635 | Easy | 4,736 |
https://leetcode.com/problems/add-digits/discuss/1882530/Python-Simple-and-Elegant!-Multiple-Solutions! | class Solution:
def addDigits(self, num):
return num % 9 or 9 if num != 0 else 0 | add-digits | Python - Simple and Elegant! Multiple Solutions! | domthedeveloper | 0 | 64 | add digits | 258 | 0.635 | Easy | 4,737 |
https://leetcode.com/problems/add-digits/discuss/1880976/Easy-to-understand-for-the-very-beginner | class Solution:
def addDigits(self, num: int) -> int:
div = num // 10
rem = num % 10
while num > 0:
if div+rem >=10:
num = div + rem
div = num // 10
rem = num % 10
else:
return div+rem
return 0 | add-digits | Easy to understand for the very beginner | nahiyan_nabil | 0 | 21 | add digits | 258 | 0.635 | Easy | 4,738 |
https://leetcode.com/problems/add-digits/discuss/1852122/Python-Solution-using-Recursion-or-Faster-than-93 | class Solution:
def addDigits(self, num: int) -> int:
if num < 10:
return num
else:
s = 0
while num > 0:
s += num % 10
num = num // 10
return self.addDigits(s) | add-digits | Python Solution using Recursion | Faster than 93% | sayantanis23 | 0 | 52 | add digits | 258 | 0.635 | Easy | 4,739 |
https://leetcode.com/problems/add-digits/discuss/1849490/Recursion-not-working-in-Add-Digits | class Solution:
def addDigits(self, num: int) -> int:
a= list(map(int, str(num)))
result = sum(a)
if result > 9:
result = addDigits(self, result)
return result
else:
return result | add-digits | Recursion not working in Add Digits | KSJ-SINGH | 0 | 9 | add digits | 258 | 0.635 | Easy | 4,740 |
https://leetcode.com/problems/add-digits/discuss/1840663/Python-Easy-Recursion-Method-(93-faster) | class Solution:
def addDigits(self, num: int) -> int:
arr = [ int(v) for v in str(num)]
if len(arr) == 1:
return arr[0]
return self.addDigits(sum(arr)) | add-digits | Python Easy Recursion Method (93% faster) | hardik097 | 0 | 42 | add digits | 258 | 0.635 | Easy | 4,741 |
https://leetcode.com/problems/add-digits/discuss/1832979/python-very-easy-solution | class Solution:
def addDigits(self, num: int) -> int:
while(len(str(num))!=1):
digits = [int(i) for i in str(num)]
num = sum(digits)
return num | add-digits | python very easy solution | akshattrivedi9 | 0 | 39 | add digits | 258 | 0.635 | Easy | 4,742 |
https://leetcode.com/problems/add-digits/discuss/1756214/3-Liner | class Solution:
def addDigits(self, num: int) -> int:
while num > 9:
num = sum(list(map(int,list(str(num)))))
return num | add-digits | 3 Liner | dineshkumar181094 | 0 | 12 | add digits | 258 | 0.635 | Easy | 4,743 |
https://leetcode.com/problems/add-digits/discuss/1755141/Very-easy-for-beginner | class Solution:
def addDigits(self, num: int) -> int:
num = str(num)
while len(num)>1:
somme = 0
for i in num:
somme += int(i)
num = str(somme)
return num | add-digits | Very easy for beginner | rajoelisonainatiavina | 0 | 9 | add digits | 258 | 0.635 | Easy | 4,744 |
https://leetcode.com/problems/add-digits/discuss/1755070/one-Line | class Solution:
def addDigits(self, num: int) -> int:`
while (num >= 10):
#1234
#127 19 10 1
num =(num %10) + (num // 10)
#print(num)
return num | add-digits | one Line | Abanob_morgan | 0 | 21 | add digits | 258 | 0.635 | Easy | 4,745 |
https://leetcode.com/problems/add-digits/discuss/1754697/Python3-Three-Aproaches-ITERATIVE-RECURSIVE-SIMPLE-MATH-TRICK | class Solution:
def addDigits(self, num: int) -> int:
while num > 9:
sum = 0
while num:
sum += num%10
num //= 10
num = sum
return num | add-digits | [Python3] Three Aproaches - ITERATIVE, RECURSIVE, SIMPLE MATH TRICK | SaiKiranMukka | 0 | 22 | add digits | 258 | 0.635 | Easy | 4,746 |
https://leetcode.com/problems/add-digits/discuss/1754697/Python3-Three-Aproaches-ITERATIVE-RECURSIVE-SIMPLE-MATH-TRICK | class Solution:
def addDigits(self, num: int) -> int:
sum = 0
while num:
sum += num%10
num //= 10
if sum < 10:
return sum
return self.addDigits(sum) | add-digits | [Python3] Three Aproaches - ITERATIVE, RECURSIVE, SIMPLE MATH TRICK | SaiKiranMukka | 0 | 22 | add digits | 258 | 0.635 | Easy | 4,747 |
https://leetcode.com/problems/add-digits/discuss/1754697/Python3-Three-Aproaches-ITERATIVE-RECURSIVE-SIMPLE-MATH-TRICK | class Solution:
def addDigits(self, num: int) -> int:
if num == 0:
return 0
elif num % 9 == 0:
return 9
return num % 9 | add-digits | [Python3] Three Aproaches - ITERATIVE, RECURSIVE, SIMPLE MATH TRICK | SaiKiranMukka | 0 | 22 | add digits | 258 | 0.635 | Easy | 4,748 |
https://leetcode.com/problems/add-digits/discuss/1754622/Python-Simple-Python-Solution-Using-Math-and-Iterative-Approach | class Solution:
def addDigits(self, num: int) -> int:
if num == 0:
return 0
if num % 9 == 0:
return 9
return num % 9 | add-digits | [ Python ] ββ Simple Python Solution Using Math and Iterative Approach | ASHOK_KUMAR_MEGHVANSHI | 0 | 28 | add digits | 258 | 0.635 | Easy | 4,749 |
https://leetcode.com/problems/add-digits/discuss/1754622/Python-Simple-Python-Solution-Using-Math-and-Iterative-Approach | class Solution:
def addDigits(self, num: int) -> int:
s=num
while s>9:
s=0
while num>0:
s=s+num%10
num=num//10
num=s
return s | add-digits | [ Python ] ββ Simple Python Solution Using Math and Iterative Approach | ASHOK_KUMAR_MEGHVANSHI | 0 | 28 | add digits | 258 | 0.635 | Easy | 4,750 |
https://leetcode.com/problems/add-digits/discuss/1754532/Python-2-solutions | class Solution:
def addDigits(self, num: int) -> int:
digital_root = 0
while num > 0:
digital_root += num % 10
num //= 10
if num == 0 and digital_root > 9:
num = digital_root
digital_root = 0
return digital_root | add-digits | Python 2 solutions | pradeep288 | 0 | 26 | add digits | 258 | 0.635 | Easy | 4,751 |
https://leetcode.com/problems/add-digits/discuss/1754532/Python-2-solutions | class Solution:
def addDigits(self, num: int) -> int:
if num<9:
return num
if num % 9 == 0:
return 9
return num % 9 | add-digits | Python 2 solutions | pradeep288 | 0 | 26 | add digits | 258 | 0.635 | Easy | 4,752 |
https://leetcode.com/problems/add-digits/discuss/1754287/simple | class Solution:
def addDigits(self, num: int) -> int:
if(num<=9):
return num;
if(num%9==0):
return 9;
return num%9; | add-digits | simple | deadlogic | 0 | 18 | add digits | 258 | 0.635 | Easy | 4,753 |
https://leetcode.com/problems/single-number-iii/discuss/2828366/brute-force-with-dictnory | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
dc=defaultdict(lambda:0)
for a in(nums):
dc[a]+=1
ans=[]
for a in dc:
if(dc[a]==1):
ans.append(a)
return ans | single-number-iii | brute force with dictnory | droj | 5 | 30 | single number iii | 260 | 0.675 | Medium | 4,754 |
https://leetcode.com/problems/single-number-iii/discuss/2368937/Python-Fast-using-counters | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
x = Counter(nums)
return([y for y in x if x[y] == 1]) | single-number-iii | Python Fast using counters | Yodawgz0 | 2 | 85 | single number iii | 260 | 0.675 | Medium | 4,755 |
https://leetcode.com/problems/single-number-iii/discuss/759337/Python3-O(N)-solution | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
diff = reduce(xor, nums)
diff &= -diff #retain last set bit
ans = [0]*2
for x in nums: ans[bool(diff & x)] ^= x
return ans | single-number-iii | [Python3] O(N) solution | ye15 | 2 | 252 | single number iii | 260 | 0.675 | Medium | 4,756 |
https://leetcode.com/problems/single-number-iii/discuss/759337/Python3-O(N)-solution | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
val = 0
for x in nums: val ^= x
val &= -val
ans = [0, 0]
for x in nums:
if x&val: ans[0] ^= x
else: ans[1] ^= x
return ans | single-number-iii | [Python3] O(N) solution | ye15 | 2 | 252 | single number iii | 260 | 0.675 | Medium | 4,757 |
https://leetcode.com/problems/single-number-iii/discuss/2029192/Python-easy-solution-using-Counter() | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
freq = Counter(nums)
return [x for x in freq if freq[x] == 1] | single-number-iii | Python easy solution using Counter() | alishak1999 | 1 | 89 | single number iii | 260 | 0.675 | Medium | 4,758 |
https://leetcode.com/problems/single-number-iii/discuss/1395157/simple-python-solution | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
s = sum(nums) - 2 * sum(set(nums))
s *= -1
for i in nums:
if s - i in nums:
return[i, s-i] | single-number-iii | simple python solution | TovAm | 1 | 101 | single number iii | 260 | 0.675 | Medium | 4,759 |
https://leetcode.com/problems/single-number-iii/discuss/1312984/Why-is-this-wrong | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
n = len(nums)
res = set(nums)
s = []
for i in range(n):
x = nums[i]%n
nums[x] = nums[x] + n
for i in range(n):
if nums[i]>=n*2:
s.append(i)
return [x for x in res if x not in s] | single-number-iii | Why is this wrong? | harshitkd | 1 | 82 | single number iii | 260 | 0.675 | Medium | 4,760 |
https://leetcode.com/problems/single-number-iii/discuss/1135562/Short-and-easy-solution-in-python | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
fin = []
for i in range (len(nums)):
if nums.count(nums[i])==1:
fin.append(nums[i])
return(fin) | single-number-iii | Short and easy solution in python | sumit17111 | 1 | 166 | single number iii | 260 | 0.675 | Medium | 4,761 |
https://leetcode.com/problems/single-number-iii/discuss/506466/Python3-both-hash-table-and-bit-manipulation-solutions | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
mask=0
for num in nums:
mask ^= num
a,diff=0,mask&(-mask)
for num in nums:
if num&diff: a^=num
return [a,mask^a]
def singleNumber1(self, nums: List[int]) -> List[int]:
ht=collections.defaultdict(int)
for num in nums:
ht[num]+=1
res=[]
for key in ht:
if ht[key]==1: res.append(key)
return res | single-number-iii | Python3 both hash table and bit manipulation solutions | jb07 | 1 | 142 | single number iii | 260 | 0.675 | Medium | 4,762 |
https://leetcode.com/problems/single-number-iii/discuss/2827258/Understand-all-approach-in-details | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
d = {} #this will contain elements corresponding to its frequency
answer = [] #this will hold our answer
#let us populate our hashtable now
for i in nums:
if i in d:
d[i] += 1
else:
d[i] = 1
for element in d:
if d[element] == 1: #it means frequency of element is 1
answer.append(element)
return answer | single-number-iii | Understand all approach in details | aarushsharmaa | 0 | 2 | single number iii | 260 | 0.675 | Medium | 4,763 |
https://leetcode.com/problems/single-number-iii/discuss/2827258/Understand-all-approach-in-details | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
nums.sort()
ans = []
n = len(nums)
for i in range(0, len(nums)):
if i == len(nums) - 1:
if nums[i] != nums[i - 1]:
ans.append(nums[i])
elif nums[i] != nums[i + 1] and nums[i] != nums[i - 1]:
ans.append(nums[i])
return ans | single-number-iii | Understand all approach in details | aarushsharmaa | 0 | 2 | single number iii | 260 | 0.675 | Medium | 4,764 |
https://leetcode.com/problems/single-number-iii/discuss/2823385/Python3-easy-to-understand | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
res = []
for k, v in Counter(nums).items():
if v == 1:
res.append(k)
return res | single-number-iii | Python3 - easy to understand | mediocre-coder | 0 | 4 | single number iii | 260 | 0.675 | Medium | 4,765 |
https://leetcode.com/problems/single-number-iii/discuss/2780933/Counter-oror-Python3 | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
ans = []
c = Counter(nums)
for key in c:
if c[key] == 1:
ans.append(key)
return ans | single-number-iii | Counter || Python3 | joshua_mur | 0 | 3 | single number iii | 260 | 0.675 | Medium | 4,766 |
https://leetcode.com/problems/single-number-iii/discuss/2732325/Easy-solution-with-dictionary-oror-Beats-99 | class Solution:
def singleNumber(self, nums: List[int]) -> int:
if len(nums) == 1: return nums[0]
d = {}
for i in nums:
if i not in d:
d[i] = 1
else:
d[i] += 1
l = []
c = 0
for j in d:
if d[j] == 1:
c += 1
l.append(j)
if c == 2:
return l | single-number-iii | Easy solution with dictionary || Beats 99% | MockingJay37 | 0 | 2 | single number iii | 260 | 0.675 | Medium | 4,767 |
https://leetcode.com/problems/single-number-iii/discuss/2594621/PYTHON3-FASTER-THAN-95-62ms-LINEAR-TIME | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
if len(nums) == 0:
return
if len(nums) == 1:
return nums
my_dict = dict.fromkeys(nums,0)
for num in nums:
my_dict[num] += 1
return [k for k, v in my_dict.items() if v == 1] | single-number-iii | [PYTHON3] FASTER THAN 95% 62ms LINEAR TIME | MariosCh | 0 | 52 | single number iii | 260 | 0.675 | Medium | 4,768 |
https://leetcode.com/problems/single-number-iii/discuss/2549940/Simple-XOR-solution-or-PYTHON | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
xor = reduce(lambda a,b: a^b, nums)
pos = 0
i = 0
temp = xor
while xor:
if xor&1:
pos = i
break
xor >>= 1
i += 1
a, b = [], []
for i in nums:
if i&(1<<pos):
a.append(i)
else:
b.append(i)
p = temp ^ reduce(lambda x,y: x^y, a)
q = temp ^ p
return [p,q] | single-number-iii | Simple XOR solution | PYTHON | RajatGanguly | 0 | 53 | single number iii | 260 | 0.675 | Medium | 4,769 |
https://leetcode.com/problems/single-number-iii/discuss/2505043/Python-or-Easy-when-using-collections.Counter | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
if(len(nums) <= 2):
return nums
res = Counter(nums).most_common()
res = [k[0] for k in res]
return res[-1], res[-2] | single-number-iii | Python | Easy when using collections.Counter | Wartem | 0 | 18 | single number iii | 260 | 0.675 | Medium | 4,770 |
https://leetcode.com/problems/single-number-iii/discuss/2474568/Python-Two-Solutions%3A-Bit-Manipulation-and-Set-oror-Documented | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
# find XOR of integers appearing once, say x and y
xy = 0
for num in nums:
xy ^= num
# find right-most set bit (1) e.g. 000010, or 00001
bit = xy & -xy
# based on this set bit create two groups, x and y will fall into two difeerent groups
# perform XOR of groups, save answer into result list
result = [0,0]
for num in nums:
if (num & bit):
result[0] ^= num # perform XOR of numbers falling into group-1
else:
result[1] ^= num # perform XOR of numbers falling into group-2
return result
def singleNumber(self, nums: List[int]) -> List[int]:
s = set() # for fast access time - O(1)
for num in nums:
if num in s:
s.remove(num) # already seen remove from set
else:
s.add(num) # not seen, add into set
return list(s) | single-number-iii | [Python] Two Solutions: Bit Manipulation and Set || Documented | Buntynara | 0 | 31 | single number iii | 260 | 0.675 | Medium | 4,771 |
https://leetcode.com/problems/single-number-iii/discuss/2153956/Simple-Logic | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
freq = Counter(nums)
return [x for x in freq if freq[x] ==1] | single-number-iii | Simple Logic | writemeom | 0 | 52 | single number iii | 260 | 0.675 | Medium | 4,772 |
https://leetcode.com/problems/single-number-iii/discuss/2153956/Simple-Logic | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
return [x for x in nums if nums.count(x)==1] | single-number-iii | Simple Logic | writemeom | 0 | 52 | single number iii | 260 | 0.675 | Medium | 4,773 |
https://leetcode.com/problems/single-number-iii/discuss/2152342/Python-oneliner | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
return [x for x in nums if nums.count(x) == 1] | single-number-iii | Python oneliner | StikS32 | 0 | 51 | single number iii | 260 | 0.675 | Medium | 4,774 |
https://leetcode.com/problems/single-number-iii/discuss/2109182/Single-Number-3-(easy-solution) | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
dic={}
res=[]
for i in nums:
if i in dic:
dic[i]+=1
else:
dic[i]=1
for i,j in dic.items():
if j==1:
res.append(i)
return res | single-number-iii | Single Number 3 (easy solution) | anil5829354 | 0 | 33 | single number iii | 260 | 0.675 | Medium | 4,775 |
https://leetcode.com/problems/single-number-iii/discuss/1854771/Python-Solution-(HashMap-) | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
h={}
ans=[]
for i in nums:
if i not in h:
h[i]=1
else:
h[i]+=1
for k,v in h.items():
if(v==1):
ans.append(k)
return ans | single-number-iii | Python Solution (HashMap ) | shalini47choudhary | 0 | 45 | single number iii | 260 | 0.675 | Medium | 4,776 |
https://leetcode.com/problems/single-number-iii/discuss/1562076/pythonc%2B%2B-Simple-Implementation | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
return [num for num in nums if nums.count(num) == 1] | single-number-iii | [python/c++] Simple Implementation | gl_9 | 0 | 27 | single number iii | 260 | 0.675 | Medium | 4,777 |
https://leetcode.com/problems/single-number-iii/discuss/1562054/Detailed-Explanation-oror-Both-Method-100-faster | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
dic = Counter(nums)
res = []
for k in dic:
if dic[k]==1:
res.append(k)
return res | single-number-iii | ππ Detailed Explanation || Both-Method }} 100% faster π | abhi9Rai | 0 | 61 | single number iii | 260 | 0.675 | Medium | 4,778 |
https://leetcode.com/problems/single-number-iii/discuss/1562054/Detailed-Explanation-oror-Both-Method-100-faster | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
axorb = 0
for n in nums:
axorb ^= n
rightsetbit = axorb & -axorb
A = 0
for n in nums:
if (n & rightsetbit)!=0:
A ^= n
return [A, A^axorb] | single-number-iii | ππ Detailed Explanation || Both-Method }} 100% faster π | abhi9Rai | 0 | 61 | single number iii | 260 | 0.675 | Medium | 4,779 |
https://leetcode.com/problems/single-number-iii/discuss/1561885/Very-easy-python-solution. | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
dict={}
arr=[]
for i in nums:
dict[i]=dict.get(i,0)+1
for i in dict:
if dict[i]==1:
arr.append(i)
return arr | single-number-iii | Very easy python solution. | Manish010 | 0 | 71 | single number iii | 260 | 0.675 | Medium | 4,780 |
https://leetcode.com/problems/single-number-iii/discuss/750779/Python3-Simple-Beats-99 | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
d = defaultdict(int)
for i in range(len(nums)):
if d[nums[i]] == 1:
del d[nums[i]]
else:
d[nums[i]] += 1
return d.keys() | single-number-iii | Python3 Simple Beats 99% | jacksilver | 0 | 241 | single number iii | 260 | 0.675 | Medium | 4,781 |
https://leetcode.com/problems/single-number-iii/discuss/1304090/Python3-solution-using-bit-manipulation | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
res = [0,0]
xor = 0
for i in nums:
xor ^= i
xor &= -xor
for i in nums:
if xor & i == 0:
res[0] ^= i
else:
res[1] ^= i
return res | single-number-iii | Python3 solution using bit manipulation | EklavyaJoshi | -1 | 80 | single number iii | 260 | 0.675 | Medium | 4,782 |
https://leetcode.com/problems/single-number-iii/discuss/352893/Solution-in-Python-3-(beats-~100) | class Solution:
def singleNumber(self, N: List[int]) -> int:
L, d = len(N), set()
for n in N:
if n in d: d.remove(n)
else: d.add(n)
return d
- Junaid Mansuri
(LeetCode ID)@hotmail.com | single-number-iii | Solution in Python 3 (beats ~100%) | junaidmansuri | -1 | 460 | single number iii | 260 | 0.675 | Medium | 4,783 |
https://leetcode.com/problems/single-number-iii/discuss/1665140/Python3-i'm-too-lazy | class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
if not nums:
return []
counter = collections.Counter(nums)
res = sorted(counter, key=lambda x:counter[x])
return [res[0], res[1]] | single-number-iii | Python3 i'm too lazy | capis2256 | -2 | 85 | single number iii | 260 | 0.675 | Medium | 4,784 |
https://leetcode.com/problems/ugly-number/discuss/336227/Solution-in-Python-3-(beats-~99)-(five-lines) | class Solution:
def isUgly(self, num: int) -> bool:
if num == 0: return False
while num % 5 == 0: num /= 5
while num % 3 == 0: num /= 3
while num % 2 == 0: num /= 2
return num == 1
- Junaid Mansuri | ugly-number | Solution in Python 3 (beats ~99%) (five lines) | junaidmansuri | 18 | 2,600 | ugly number | 263 | 0.426 | Easy | 4,785 |
https://leetcode.com/problems/ugly-number/discuss/1764482/Python3-or-Faster-solution-Basic-logic-with-maths | class Solution:
def isUgly(self, n: int) -> bool:
if n<=0:
return False
for i in [2,3,5]:
while n%i==0:
n=n//i
return n==1 | ugly-number | β Python3 | Faster solution , Basic logic with maths | Anilchouhan181 | 13 | 843 | ugly number | 263 | 0.426 | Easy | 4,786 |
https://leetcode.com/problems/ugly-number/discuss/2825633/in-log(n)-time | class Solution:
def isUgly(self, n: int) -> bool:
while(n%2==0 and n!=0):
n=n//2
while(n%3==0 and n!=0):
n=n//3
while(n%5==0 and n!=0):
n=n//5
return(n==1) | ugly-number | in log(n) time | droj | 8 | 303 | ugly number | 263 | 0.426 | Easy | 4,787 |
https://leetcode.com/problems/ugly-number/discuss/2345474/Python-93.76-faster-or-Simplest-solution-with-explanation-or-Beg-to-Adv-or-Math | class Solution:
def isUgly(self, n: int) -> bool:
prime = [2, 3, 5] # prime factors list provided in question againt which we have to check the provided number.
if n == 0: # as we dont have factors for 0
return False
for p in prime: # traversing prime numbers from given prime number list.
while n % p == 0: # here we`ll check if the number is having the factor or not. For instance 6%2==0 is true implies 2 is a factor of 6.
n //= p # num = num//p # in this we`ll be having 3(6/2), 1(3/3). Doing this division to update our number
return n == 1 # at last we`ll always have 1, if the number would have factors from the provided list | ugly-number | Python 93.76% faster | Simplest solution with explanation | Beg to Adv | Math | rlakshay14 | 3 | 260 | ugly number | 263 | 0.426 | Easy | 4,788 |
https://leetcode.com/problems/ugly-number/discuss/2345474/Python-93.76-faster-or-Simplest-solution-with-explanation-or-Beg-to-Adv-or-Math | class Solution:
def isUgly(self, n: int) -> bool:
if n == 0: return False;
while n%2 == 0:
n /= 2
while n%3 == 0:
n /= 3
while n%5 == 0:
n /= 5
return n == 1 | ugly-number | Python 93.76% faster | Simplest solution with explanation | Beg to Adv | Math | rlakshay14 | 3 | 260 | ugly number | 263 | 0.426 | Easy | 4,789 |
https://leetcode.com/problems/ugly-number/discuss/1622569/while-loop-in-Python-beats-89.09 | class Solution:
def isUgly(self, n: int) -> bool:
def divide_all(divisor):
nonlocal n
while n > 1 and n % divisor == 0:
n //= divisor
#if n <= 0, always False
if n < 1: return False
#divide by 2 and 3 and 5 while you can divide
divide_all(2); divide_all(3); divide_all(5)
return n == 1 | ugly-number | while loop in Python, beats 89.09% | kryuki | 3 | 302 | ugly number | 263 | 0.426 | Easy | 4,790 |
https://leetcode.com/problems/ugly-number/discuss/1236389/Python3-simple-solution-beats-99-users | class Solution:
def isUgly(self, n: int) -> bool:
if n <= 0:
return False
if n == 1:
return True
while n != 1:
if n % 2 == 0:
n //= 2
elif n % 3 == 0:
n //= 3
elif n % 5 == 0:
n //= 5
else:
return False
return True | ugly-number | Python3 simple solution beats 99% users | EklavyaJoshi | 3 | 185 | ugly number | 263 | 0.426 | Easy | 4,791 |
https://leetcode.com/problems/ugly-number/discuss/1103723/Faster-than-99.34-Python-Solution-(depends-on-test-cases-it-will-vary) | class Solution:
def isUgly(self, n: int) -> bool:
while n>0:
if n==1:
return True
if n%2==0:
n=n//2
elif n%3==0:
n=n//3
elif n%5==0:
n=n//5
else:
return False
return False | ugly-number | Faster than 99.34 % Python Solution (depends on test cases it will vary) | lalith_kumaran | 3 | 413 | ugly number | 263 | 0.426 | Easy | 4,792 |
https://leetcode.com/problems/ugly-number/discuss/2826238/Simple-python-solution | class Solution:
def isUgly(self, n: int) -> bool:
if n <= 0:
return False
factors = [5, 3, 2]
for factor in factors:
while n % factor == 0:
n //= factor
return n == 1 | ugly-number | Simple python solution | ibogretsov | 2 | 60 | ugly number | 263 | 0.426 | Easy | 4,793 |
https://leetcode.com/problems/ugly-number/discuss/2826092/Simple-Python-solution-with-explanation | class Solution:
def isUgly(self, n: int) -> bool:
if n == 0: return False
sieve = (2, 3, 5)
while n != 1:
for f in sieve:
if n % f == 0:
n = n // f
break
else:
return False
return True | ugly-number | π‘Simple Python solution with explanation | gp_os | 2 | 80 | ugly number | 263 | 0.426 | Easy | 4,794 |
https://leetcode.com/problems/ugly-number/discuss/2629644/Python-or-Easy-to-Understand-or-Clean | class Solution:
def isUgly(self, n: int) -> bool:
if n <= 0: return False
for i in [2,3,5] :
while n % i==0 :
n = n//i
return n==1 | ugly-number | Python | Easy to Understand | Clean | shahidmu | 2 | 363 | ugly number | 263 | 0.426 | Easy | 4,795 |
https://leetcode.com/problems/ugly-number/discuss/2087024/Python3-Runtime%3A-40ms-6558 | class Solution:
# O(n) || O(1)
# 40ms 65.68%
def isUgly(self, num: int) -> bool:
if num <= 0:
return False
for i in [2, 3, 5]:
while num % i == 0:
num //= i
return num == 1 | ugly-number | Python3 Runtime: 40ms 65,58% | arshergon | 2 | 193 | ugly number | 263 | 0.426 | Easy | 4,796 |
https://leetcode.com/problems/ugly-number/discuss/1262257/Easy-Python-Solution | class Solution:
def isUgly(self, n: int) -> bool:
if(n<=0):
return False
if(n==1):
return True
while(n>1):
if(n%2==0):
n=n/2
elif(n%3==0):
n=n/3
elif(n%5==0):
n=n/5
else:
return False
return True | ugly-number | Easy Python Solution | Sneh17029 | 2 | 517 | ugly number | 263 | 0.426 | Easy | 4,797 |
https://leetcode.com/problems/ugly-number/discuss/719320/Python3-math-and-dp | class Solution:
def isUgly(self, n: int) -> bool:
if n <= 0: return False
for f in 2,3,5:
while n%f == 0: n //= f
return n == 1 | ugly-number | [Python3] math & dp | ye15 | 2 | 195 | ugly number | 263 | 0.426 | Easy | 4,798 |
https://leetcode.com/problems/ugly-number/discuss/719320/Python3-math-and-dp | class Solution:
def isUgly(self, num: int) -> bool:
if num <= 0: return False #edge case
@cache
def fn(x):
"""Return True if x is an ugly number"""
if x == 1: return True
return any(x % f == 0 and fn(x//f) for f in (2, 3, 5))
return fn(num) | ugly-number | [Python3] math & dp | ye15 | 2 | 195 | ugly number | 263 | 0.426 | Easy | 4,799 |
Subsets and Splits