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/excel-sheet-column-number/discuss/1791008/Python-3-(50ms)-or-ord-Math-Solution-or-Easy-to-Understand | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
r=0
for i in columnTitle:
r = r * 26 + ord(i)-64
return r | excel-sheet-column-number | Python 3 (50ms) | ord Math Solution | Easy to Understand | MrShobhit | 3 | 146 | excel sheet column number | 171 | 0.614 | Easy | 2,800 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/1793622/Python3-oror-fast-solution-or-using-maths | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
result_num = 0
power = 0
A=0
len_alphabet = len(alphabet)
while len(columnTitle) > 0:
position = alphabet.find(columnTitle[-1]) + 1
columnTitle = columnTitle[0: -1]
A= position * len_alphabet ** power
result_num += A
power += 1
return result_num | excel-sheet-column-number | Python3 || fast solution | using maths | Anilchouhan181 | 2 | 79 | excel sheet column number | 171 | 0.614 | Easy | 2,801 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/472766/Python-3-Solution-with-Detailed-Explanation | class Solution:
#Perform subtraction of ordinal values to get how far away the character is from
#A, which translates to the value. Add 1 to account for number system starting at
#1 instead of 0.
def titleToNumber(self, s: str) -> int:
r = 0
startPwr = len(s) - 1
for char in s:
r += (ord(char) - ord('A') + 1) * 26 ** startPwr
startPwr -= 1
return r | excel-sheet-column-number | Python 3 Solution with Detailed Explanation | studcoder569 | 2 | 181 | excel sheet column number | 171 | 0.614 | Easy | 2,802 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/1790224/Python-Solutions-With-Explaination-Very-Simple | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
result = 0
for c in columnTitle:
d = ord(c) - ord('A') + 1
result = result * 26 + d
return result | excel-sheet-column-number | Python Solutions With Explaination - Very Simple | chinmayroy | 1 | 216 | excel sheet column number | 171 | 0.614 | Easy | 2,803 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/1633163/Python-Easy-Solution-or-Two-line-Approach | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
res = 0
for char in columnTitle:
res *= 26
res += ord(char)-ord('A')+1
return res | excel-sheet-column-number | Python Easy Solution | Two-line Approach ✔ | leet_satyam | 1 | 209 | excel sheet column number | 171 | 0.614 | Easy | 2,804 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/1594349/Python-3-with-step-by-step-explanation | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
value="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
res=0
for i in range(len(columnTitle)):
index_value=value.index(columnTitle[i])+1
if(i!=len(columnTitle)-1):
res=(res+index_value)*26
else:
res=res+index_value
return res | excel-sheet-column-number | Python 3 with step by step explanation | Deepika_P15 | 1 | 150 | excel sheet column number | 171 | 0.614 | Easy | 2,805 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/920721/Python-1-line-fast-with-explanation | class Solution:
def titleToNumber(self, s: str) -> int:
return sum([(26**i) * (ord(el) - ord('A') + 1) for i, el in enumerate(s[::-1])]) | excel-sheet-column-number | Python 1 line fast with explanation | modusV | 1 | 125 | excel sheet column number | 171 | 0.614 | Easy | 2,806 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/783424/Excel-Sheet-Column-Number-or-Python3-or-pythonic-style | class Solution:
def __init__(self):
self.val = {
'A': 1, 'B': 2, 'C': 3, 'D': 4,
'E': 5, 'F': 6, 'G': 7, 'H': 8,
'I': 9, 'J': 10, 'K': 11, 'L': 12,
'M': 13, 'N': 14, 'O': 15, 'P': 16,
'Q': 17, 'R': 18, 'S': 19, 'T': 20,
'U': 21, 'V': 22, 'W': 23, 'X': 24,
'Y': 25, 'Z': 26
}
def titleToNumber(self, s: str) -> int:
result = 0
for exponent, letter in enumerate(reversed(list(s))):
result += self.val[letter] * 26**exponent
return result | excel-sheet-column-number | Excel Sheet Column Number | Python3 | pythonic style | Matthias_Pilz | 1 | 107 | excel sheet column number | 171 | 0.614 | Easy | 2,807 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/729159/Python3-26-carry-system | class Solution:
def titleToNumber(self, s: str) -> int:
ans = 0
for c in s:
ans = 26*ans + ord(c) - 64
return ans | excel-sheet-column-number | [Python3] 26-carry system | ye15 | 1 | 71 | excel sheet column number | 171 | 0.614 | Easy | 2,808 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/729159/Python3-26-carry-system | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
return reduce(lambda x, y: 26*x+ord(y)-64, columnTitle, 0) | excel-sheet-column-number | [Python3] 26-carry system | ye15 | 1 | 71 | excel sheet column number | 171 | 0.614 | Easy | 2,809 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/420507/Number-system-interpretation-python | class Solution:
def titleToNumber(self, s: str) -> int:
col = 0
for i, c in enumerate(reversed(s)):
val = ord(c)-64
col += (26**i)*val
return col | excel-sheet-column-number | Number system interpretation - python | ujjwalg3 | 1 | 124 | excel sheet column number | 171 | 0.614 | Easy | 2,810 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/336165/Solution-in-Python-3-(beats-~98)-(one-line) | class Solution:
def titleToNumber(self, s: str) -> int:
return sum([(ord(c)-64)*(26**(len(s)-1-i)) for i,c in enumerate(s)])
- Python 3
- Junaid Mansuri | excel-sheet-column-number | Solution in Python 3 (beats ~98%) (one line) | junaidmansuri | 1 | 449 | excel sheet column number | 171 | 0.614 | Easy | 2,811 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2839747/Python-oror-ord-solution | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
col = list(columnTitle)
res = 0
for i in range(len(col),0,-1):
orde = ord(col[i-1])-64
if i == 1:
res += orde*26**(len(col)-i)
else:
temp = 26**(len(col)-i+1)
tempp = orde*26**(len(col)-i)
res += temp
res -= temp - tempp
return res | excel-sheet-column-number | Python || ord solution | dyussenovaanel | 0 | 3 | excel sheet column number | 171 | 0.614 | Easy | 2,812 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2838591/Python-Solution | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
colNum = 0
for ltr in columnTitle:
colNum = colNum* 26 + ord(ltr) - 64
return colNum | excel-sheet-column-number | Python Solution | yordanoswuletaw | 0 | 1 | excel sheet column number | 171 | 0.614 | Easy | 2,813 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2836251/Easiest-Python-code-understandable-by-everyone | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
a=list(columnTitle)
r=0
z=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
for i in range(len(a)):
for j in range(26):
if a[i]==z[j]:
r=r+26**(len(a)-i-1)*(j+1)
return r | excel-sheet-column-number | Easiest Python code understandable by everyone | gowdavidwan2003 | 0 | 1 | excel sheet column number | 171 | 0.614 | Easy | 2,814 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2830424/python-oror-simple-solution | class Solution:
def titleToNumber(self, t: str) -> int:
# column num
n = 0
# title len
t_len = len(t)
# go through chars in title
for i in range(t_len):
# manual multiplication with base 26
n += ((26 ** (t_len - i - 1)) * (ord(t[i]) - 64))
return n | excel-sheet-column-number | python || simple solution | wduf | 0 | 2 | excel sheet column number | 171 | 0.614 | Easy | 2,815 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2825563/Python-Simple-solution-using-ord() | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
result = 0
for i, letter in enumerate(reversed(columnTitle)):
result += (ord(letter) - ord("A") + 1) * 26 ** i
return result | excel-sheet-column-number | Python, Simple solution using ord() | retroruk | 0 | 3 | excel sheet column number | 171 | 0.614 | Easy | 2,816 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2819586/python-beats-97.98-or | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
result = 0
for i, val in enumerate(columnTitle):
if i+1 == len(columnTitle):
result += ord(val)-64
break
result += (ord(val)-64) * 26**(len(columnTitle)-i-1)
return result | excel-sheet-column-number | [python] - beats 97.98% | | ceolantir | 0 | 2 | excel sheet column number | 171 | 0.614 | Easy | 2,817 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2810164/Fast-and-easy-solution | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
columnTitle = columnTitle[::-1]
number = 0
for i in range(len(columnTitle)):
number += (ord(columnTitle[i]) - 64) * (26 ** i)
return number | excel-sheet-column-number | Fast and easy solution | vkamenin | 0 | 5 | excel sheet column number | 171 | 0.614 | Easy | 2,818 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2797575/Simple-Python3-Solution | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
result = 0
for char in columnTitle:
result = result * 26 + (ord(char) - 64)
return result | excel-sheet-column-number | Simple Python3 Solution | vivekrajyaguru | 0 | 2 | excel sheet column number | 171 | 0.614 | Easy | 2,819 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2792267/Python3 | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
base = ord('A') - 1
l = len(columnTitle) - 1
r = 0
for c in columnTitle:
r += (ord(c) - base) * pow(26, l)
l -= 1
return r | excel-sheet-column-number | Python3 | LeeXun | 0 | 1 | excel sheet column number | 171 | 0.614 | Easy | 2,820 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2786984/171.-Excel-Sheet-Column-Number-Python-or-Java | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
res=0
for n,i in enumerate(columnTitle[::-1]):
res+=(math.pow(26,n)*(ord(i)-64))
return int(res) | excel-sheet-column-number | 171. Excel Sheet Column Number [Python | Java] | IlamaranMagesh | 0 | 2 | excel sheet column number | 171 | 0.614 | Easy | 2,821 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2786573/Python-linear | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
mapper = {}
letters = string.ascii_uppercase
for ix, char in enumerate(letters):
mapper[char]=ix+1
res = 0
for i,v in enumerate(columnTitle[::-1]):
res += (26 ** i) * (mapper.get(v))
return res | excel-sheet-column-number | Python linear | champ- | 0 | 3 | excel sheet column number | 171 | 0.614 | Easy | 2,822 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2784683/O(n)-Python-3-using-ASCII-Code-of-character | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
base = ord('A')
column_number = 0
if (len(columnTitle) == 1):
return (ord(columnTitle) - base) + 1
for char in columnTitle:
column_number *= 26
current_index = (ord(char) - base) +1
column_number += current_index
return column_number | excel-sheet-column-number | ✓ O(n) Python 3 using ASCII Code of character | divesh-panwar | 0 | 5 | excel sheet column number | 171 | 0.614 | Easy | 2,823 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2779279/Python3-Simple-Solution-with-Clean-Syntax | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
columnNumber = 0
for i, c in enumerate(columnTitle[::-1]):
columnNumber += (26**i) * (ord(c) - ord('A') + 1)
return columnNumber | excel-sheet-column-number | Python3 Simple Solution with Clean Syntax | natesgibson | 0 | 5 | excel sheet column number | 171 | 0.614 | Easy | 2,824 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2770428/Easy-and-fast-solution-Python | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
sum ,mult = 0, 1
for c in columnTitle[::-1]:
print(c)
sum += (ord(c)-64) * mult
mult *= 26
return sum | excel-sheet-column-number | Easy and fast solution - Python | Doron-Az | 0 | 1 | excel sheet column number | 171 | 0.614 | Easy | 2,825 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2764781/Python-One-Liner-or-Easy-to-understand | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
return sum([(26**i)*(ord(columnTitle[-(i+1)])-64) for i in range(len(columnTitle))]) | excel-sheet-column-number | Python One Liner | Easy to understand | shreyans | 0 | 1 | excel sheet column number | 171 | 0.614 | Easy | 2,826 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2764447/Python-3-Solution | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
res = 0
for x in columnTitle:
res *= 26
res += (int(ord(x)) - 64)
return res | excel-sheet-column-number | Python 3 Solution | mati44 | 0 | 2 | excel sheet column number | 171 | 0.614 | Easy | 2,827 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2746944/Python3or-Faster-than-90.05-or-Easy-To-UnderStand | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
result = 0
position = len(columnTitle)-1
for char in(columnTitle):
digit = ord(char) - 64
result+= digit * 26 ** position
position-=1
return result | excel-sheet-column-number | Python3| Faster than 90.05% | Easy To UnderStand | rishabh_055 | 0 | 5 | excel sheet column number | 171 | 0.614 | Easy | 2,828 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2745933/Python3-Principle-of-counting | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
num, total = len(columnTitle)-1, 0
for s in columnTitle:
total += (ord(s) - 64)*(26)**num
num -= 1
return total | excel-sheet-column-number | Python3, Principle of counting | paul1202 | 0 | 2 | excel sheet column number | 171 | 0.614 | Easy | 2,829 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2739768/Easy-Fast-Python | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
num = 0
capitals = [chr(x) for x in range(ord('A'), ord('Z') + 1)]
for i, x in enumerate(columnTitle):
num += (capitals.index(x)+1) * pow(26, len(columnTitle)-i-1)
return num | excel-sheet-column-number | Easy-Fast Python | don_masih | 0 | 1 | excel sheet column number | 171 | 0.614 | Easy | 2,830 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2715980/Fast-and-Easy-O(N)-Solution | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
count = 0
for i in columnTitle:
count *= 26
count += (ord(i) - 64)
return count | excel-sheet-column-number | Fast and Easy O(N) Solution | user6770yv | 0 | 3 | excel sheet column number | 171 | 0.614 | Easy | 2,831 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2698880/Very-easy-solution-PYTHON | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
ans = 0
for i in range(len(columnTitle)):
ans += (ord(columnTitle[-1])-64) * 26**i
columnTitle = columnTitle[:len(columnTitle)-1]
return ans | excel-sheet-column-number | Very easy solution PYTHON | manufesto | 0 | 2 | excel sheet column number | 171 | 0.614 | Easy | 2,832 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2670156/Python-1-liner | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
return sum([(26 ** i) * (ord(c) - 64) for i, c in enumerate(reversed(columnTitle))]) | excel-sheet-column-number | Python 1 liner | phantran197 | 0 | 3 | excel sheet column number | 171 | 0.614 | Easy | 2,833 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2505731/Easy-Python-Solution | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
ans=0
for ch in columnTitle:
ans = ans*26 + ord(ch)-ord("A")+1
return ans | excel-sheet-column-number | Easy Python Solution | miyachan | 0 | 19 | excel sheet column number | 171 | 0.614 | Easy | 2,834 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2424018/python-easy | class Solution(object):
def titleToNumber(self, columnTitle):
pw = len(columnTitle)-1
x= 0
for i in columnTitle:
x += (26**pw)*(ord(i) - 64);
pw-=1
return x | excel-sheet-column-number | python easy | shanks733 | 0 | 18 | excel sheet column number | 171 | 0.614 | Easy | 2,835 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2354938/Python-using-ord | class Solution:
def titleToNumber(self, c: str) -> int:
f = 0
for i,ch in enumerate(c[::-1]):
if i == 0:
f = (ord(ch)-64)
continue
f += (ord(ch)-64)*pow(26,i)
return(f) | excel-sheet-column-number | Python using ord | Yodawgz0 | 0 | 34 | excel sheet column number | 171 | 0.614 | Easy | 2,836 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2251508/Memory-Usage-less-than-95.64 | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
res= 0
for i in columnTitle:
a = ord(i)-ord("A")+1
res = res*26 + a
return res | excel-sheet-column-number | Memory Usage less than -95.64% | jayeshvarma | 0 | 83 | excel sheet column number | 171 | 0.614 | Easy | 2,837 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2232950/Python-Easy-to-Understand | class Solution(object):
def titleToNumber(self, s):
res = 0
j = len(s)-1
for i in s:
res = res + (ord(i) - ord("A") + 1) * 26**j
j = j-1
return res | excel-sheet-column-number | Python - Easy to Understand | Sharath1996 | 0 | 150 | excel sheet column number | 171 | 0.614 | Easy | 2,838 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2174619/Python-solution | class Solution:
def titleToNumber(self, title: str) -> int:
table = dict(zip(ascii_uppercase, range(1, 27)))
return sum(table[title[~i]] * 26 ** i for i in range(len(title))) | excel-sheet-column-number | Python solution | siavrez | 0 | 30 | excel sheet column number | 171 | 0.614 | Easy | 2,839 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2155378/Easy-to-understand-python-solution-%3A) | class Solution(object):
def titleToNumber(self, columnTitle):
"""
:type columnTitle: str
:rtype: int
"""
placenum= len(columnTitle)-1 # position value of 1st digit from right
ans=0
for char in columnTitle:
placevalue= 26**placenum
facevalue= ord(char)-(ord('A')-1) # to get values for digits(1 for A, 2 for B...)
ans= ans+ placevalue*facevalue
placenum=placenum-1
return ans | excel-sheet-column-number | Easy to understand python solution :) | sri_28 | 0 | 53 | excel sheet column number | 171 | 0.614 | Easy | 2,840 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2120129/Python-1-liner | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
return sum((ord(char) - 64) * 26 ** (len(columnTitle) - idx - 1) for idx, char in enumerate(columnTitle)) | excel-sheet-column-number | Python 1-liner | JuanRodriguez | 0 | 28 | excel sheet column number | 171 | 0.614 | Easy | 2,841 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2118571/Python-Base26-Solution | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
res = 0
digi = 0
for i in range(len(columnTitle)-1, -1, -1):
# Base 26
res += (ord(columnTitle[i])-64)*(26**digi)
digi += 1
return res | excel-sheet-column-number | Python Base26 Solution | codeee5141 | 0 | 50 | excel sheet column number | 171 | 0.614 | Easy | 2,842 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2106371/Python3-SOLUTION-EXPLAINED-. | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
res = 0
n = len(columnTitle)
for i in range(n-1,-1,-1):
code = ord(columnTitle[i])-65
res += (26**(n-1-i))*(code+1)
return res | excel-sheet-column-number | [Python3] SOLUTION EXPLAINED . | SoeRatch | 0 | 14 | excel sheet column number | 171 | 0.614 | Easy | 2,843 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2088764/Simple-python-solution | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
total = 0
for i in columnTitle:
total = total * 26 + (ord(i) - 64)
return total | excel-sheet-column-number | Simple python solution | rahulsh31 | 0 | 54 | excel sheet column number | 171 | 0.614 | Easy | 2,844 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2055706/Python3-using-while-and-if-else | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
# main idea (example)
# columnTitle = "ZY"
# columnTitle == 2
# total alphabet * (current index char + 1) + (index next char + 1)
# 26 * 26 + 25 = 701
c = list(columnTitle)
ascii = list(string.ascii_uppercase)
if len(c) == 1:
return ascii.index(c[0]) + 1
else:
i = 0
total = 1
while c:
cIndex = ascii.index(columnTitle[i]) + 1
if i == 0:
total *= 26
total *= cIndex
else:
if i == 1:
total += cIndex
else:
total *= 26
total += cIndex
i += 1
c = c[1:]
return total | excel-sheet-column-number | [Python3] using while and if else | Shiyinq | 0 | 42 | excel sheet column number | 171 | 0.614 | Easy | 2,845 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2053398/Easy-2-line-solution-beats-greater95 | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
d = {char:i+1 for i,char in enumerate("ABCDEFGHIJKLMNOPQRSTUVWXYZ")}
return functools.reduce(lambda a,b:a+b,[d[char]*(26**(len(columnTitle)-1-i)) for i,char in enumerate(columnTitle)]) | excel-sheet-column-number | Easy 2 line solution beats >95% | IsaacQ135 | 0 | 31 | excel sheet column number | 171 | 0.614 | Easy | 2,846 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/2005442/CLEAN-python-solution-(right-to-left)-better-than-solution-tab | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
total = 0
# columnTitle[::-1] reverses the array, so we read from right to left
for i, c in enumerate(columnTitle[::-1]):
val = ord(c) - ord("A") + 1 # maps each letter A -> 1, B -> 2, ..., Z -> 26
total += val * 26 ** (i)
return total | excel-sheet-column-number | CLEAN python solution (right to left) better than solution tab | jameszhang-a | 0 | 31 | excel sheet column number | 171 | 0.614 | Easy | 2,847 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/1937720/Python-3-Solution | class Solution:
def titleToNumber(self, columnTitle: str) -> int:
columnTitle = columnTitle[::-1]
sm = 0
for i in range(len(columnTitle)):
sm += (pow(26,i) * (abs(ord('A')-ord(columnTitle[i])) + 1))
return sm | excel-sheet-column-number | Python 3 Solution | DietCoke777 | 0 | 39 | excel sheet column number | 171 | 0.614 | Easy | 2,848 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/1837555/Python-One-liner-The-functional-way | class Solution(object):
def titleToNumber(self, s):
def toNumber(x): return ord(x)-ord('A')+1
def addBase26(a, b): return (a * 26) + b
return reduce(addBase26, map(toNumber, s)) | excel-sheet-column-number | Python - One liner - The functional way | domthedeveloper | 0 | 85 | excel sheet column number | 171 | 0.614 | Easy | 2,849 |
https://leetcode.com/problems/excel-sheet-column-number/discuss/1837555/Python-One-liner-The-functional-way | class Solution(object):
def titleToNumber(self, s):
return reduce(lambda a,b:a*26+b, map(lambda x:ord(x)-ord('A')+1, s)) | excel-sheet-column-number | Python - One liner - The functional way | domthedeveloper | 0 | 85 | excel sheet column number | 171 | 0.614 | Easy | 2,850 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1152167/Python3-O(log(n))-time-O(1)-space.-Explanation | class Solution:
def trailingZeroes(self, n: int) -> int:
quotient = n // 5
return quotient + self.trailingZeroes(quotient) if quotient >= 5 else quotient | factorial-trailing-zeroes | Python3 O(log(n)) time, O(1) space. Explanation | ryancodrai | 12 | 472 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,851 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/2452895/Very-Easy-oror-100-oror-Fully-Explained-oror-Java-oror-C%2B%2Boror-Python-oror-JS-oror-C-oror-Python3 | class Solution(object):
def trailingZeroes(self, n):
# Negative Number Edge Case
if(n < 0):
return -1
# Initialize output...
output = 0
# Keep dividing n by 5 & update output...
while(n >= 5):
n //= 5
output += n
return output # Return the output... | factorial-trailing-zeroes | Very Easy || 100% || Fully Explained || Java || C++|| Python || JS || C || Python3 | PratikSen07 | 4 | 404 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,852 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/279485/Python3-beats-100-36ms | class Solution:
def trailingZeroes(self, n: int) -> int:
if n<5:
return 0
x=0
while n != 0:
x += n // 5
n //= 5
return x | factorial-trailing-zeroes | Python3 beats 100%, 36ms | Lilbud_314 | 4 | 693 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,853 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1258493/WEEB-EXPLAINS-PYTHON-MATH | class Solution:
def trailingZeroes(self, n: int) -> int:
count = 0
while(n >= 5):
n //= 5 # integer division, we don't need decimal numbers
count += n
return count | factorial-trailing-zeroes | WEEB EXPLAINS PYTHON MATH | Skywalker5423 | 3 | 228 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,854 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/2751198/Python3-ororO(N) | class Solution:
def trailingZeroes(self, n: int) -> int:
f=1
if n==0 and n==1:
return 0
for i in range(1,n+1):
f*=i
s=str(f)[::-1]
count=0
for i in s:
if i=="0":
count+=1
else:
break
return count | factorial-trailing-zeroes | Python3 ||O(N) | Sneh713 | 2 | 193 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,855 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1442486/Python-oror-Easy-Solution-oror-log(n)-time-complexity | class Solution:
def trailingZeroes(self, n: int) -> int:
temp = 0
while n >= 5:
n = (n // 5)
temp += n
return temp | factorial-trailing-zeroes | Python || Easy Solution || log(n) time complexity | naveenrathore | 2 | 217 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,856 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1124356/Python3-Solution-beats-98 | class Solution:
def trailingZeroes(self, n: int) -> int:
"""Brute Force Approach
def get_factorial(n):
if n in (0,1):
return 1
else:
factorial = 1
for i in range(2,n+1):
factorial *= i
return factorial
factorial = str(get_factorial(n))[::-1]
for i in range(len(factorial)):
if factorial[i] != '0':
return (0,i)[i != 0]
"""
"""Optimal Solution"""
result = 0
while n >= 5:
n = n // 5
result += n
return result | factorial-trailing-zeroes | Python3 Solution beats 98% | malleswari1593 | 2 | 129 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,857 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1314279/Easy-and-fast-solution-_python3 | class Solution:
def trailingZeroes(self, n: int) -> int:
ans = 0
while n >= 5:
ans += n // 5 # check is it contains any 5 in it ?
n = n // 5 # continue to check any 5 hide in it.
return ans | factorial-trailing-zeroes | Easy & fast solution _python3 | An_222 | 1 | 181 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,858 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/729181/Python3-counting-fives | class Solution:
def trailingZeroes(self, n: int) -> int:
ans = 0
while n:
n //= 5
ans += n
return ans | factorial-trailing-zeroes | [Python3] counting fives | ye15 | 1 | 128 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,859 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/438363/Python-easy-solution-with-n!-counting-formula-faster-than-99.49 | class Solution:
def trailingZeroes(self, n: int) -> int:
t = 0
i = 1
while (n // 5 ** i) >= 1:
t += n // 5 ** i
i += 1
return t | factorial-trailing-zeroes | Python easy solution with n! counting formula, faster than 99.49% | jb07 | 1 | 202 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,860 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/2804812/Pyhton-or-O(n)-or-Easy-to-understand-and-manipulate | class Solution:
def trailingZeroes(self, n: int) -> int:
f = 1
ans = 0
while n > 0:
f *= n
n -= 1
f = str(f)
i = len(f) - 1
while i >= 0:
if f[i] == '0':
ans += 1
else:
break
i -= 1
return ans | factorial-trailing-zeroes | Pyhton | O(n) | Easy to understand and manipulate | bhuvneshwar906 | 0 | 6 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,861 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/2789076/For-Easy-Question-Easy-Solution | class Solution:
def trailingZeroes(self, n: int) -> int:
val = 1
for i in range(1,n+1):
val = val*i
if n==0:
return 0
else:
str1 = str(val)
count= 0
length = len(str1)
for i in range(length-1,-1,-1):
if str1[i]=="0":
count +=1
else:
break
return count | factorial-trailing-zeroes | For Easy Question Easy Solution | VINAY_KUMAR_V_C | 0 | 5 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,862 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/2749312/Python-or-Recursive-Solution | class Solution:
def trailingZeroes(self, n: int) -> int:
if n==0:
return 0
def factorial(n):
if n==1:
return 1
else:
return n*factorial(n-1)
res=str(factorial(n))
c=0
for i in res[::-1]:
if int(i) >0:
break
c+=1
return c | factorial-trailing-zeroes | Python | Recursive Solution | jainsiddharth99 | 0 | 6 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,863 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/2740143/Simple-Mathematical-Approach | class Solution:
def trailingZeroes(self, n: int) -> int:
res = 0
while n >= 5:
n //= 5
res += n
return res | factorial-trailing-zeroes | Simple Mathematical Approach | sonnylaskar | 0 | 11 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,864 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/2738344/Python-or-Fives | class Solution:
def trailingZeroes(self, n: int) -> int:
fives = 0
p_five = 5
while p_five <= n:
fives += n // p_five
p_five *= 5
return fives | factorial-trailing-zeroes | Python | Fives | on_danse_encore_on_rit_encore | 0 | 5 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,865 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/2716798/Python-O(N)-way-and-Golang-O(log5-N)-way | class Solution:
def trailingZeroes(self, n: int) -> int:
trailing_zeroes_count = 0
# Get the factorial number of n
# And convert to str.
factorial_number = str(math.factorial(n))
# left index and right index
left = 0
right = len(factorial_number) - 1
# Count trailing zero count
while left <= right:
if factorial_number[right] == '0':
trailing_zeroes_count += 1
right -= 1
else:
return trailing_zeroes_count
return trailing_zeroes_count | factorial-trailing-zeroes | Python O(N) way and Golang O(log5 N) way | namashin | 0 | 4 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,866 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/2154330/Intuitive-Python-Solution | class Solution:
def trailingZeroes(self, n: int) -> int:
count = value = floor(n / 5)
i = 1
while value != 0:
i += 1
value = floor(n / pow(5, i))
count += value
return count | factorial-trailing-zeroes | Intuitive Python Solution | kn_vardhan | 0 | 32 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,867 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/2051683/Python-simple-solution | class Solution:
def trailingZeroes(self, n: int) -> int:
from math import factorial as f
ans = 0
for i in str(f(n))[::-1]:
if i != '0':
break
ans += 1
return ans | factorial-trailing-zeroes | Python simple solution | StikS32 | 0 | 87 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,868 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1969560/Python3-or-Simple-Approach | class Solution:
def trailingZeroes(self, n: int) -> int:
count = 0
while n >= 5:
n = n // 5
count += n
return count | factorial-trailing-zeroes | Python3 | Simple Approach | goyaljatin9856 | 0 | 59 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,869 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1879019/Constant-Time-Solution | class Solution:
def trailingZeroes(self, n: int) -> int:
return n // 5 + n // 25 + n // 125 + n // 625 + n // 3125 | factorial-trailing-zeroes | Constant Time Solution | subhadeepdev | 0 | 53 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,870 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1865472/Python-3-easy-solution | class Solution:
def trailingZeroes(self, n: int) -> int:
cnt = 0
while n >= 5:
n = n // 5
cnt += n
return cnt | factorial-trailing-zeroes | Python 3 easy solution | constantine786 | 0 | 147 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,871 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1413853/Python-simple-optimized-solution | class Solution:
def trailingZeroes(self, n: int) -> int:
if n <= 4: return 0
cnt = 0
while n >= 5:
cnt += 1
n = n // 5
ans = 0
m = n
for i in range(1, cnt+1):
ans += m // (5**i)
return ans | factorial-trailing-zeroes | Python simple optimized solution | byuns9334 | 0 | 175 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,872 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1261305/Easy-Python-Solution | class Solution:
def trailingZeroes(self, n: int) -> int:
x=n
s=0
x=x//5
s+=x
while(x>=5):
x=x//5
s+=x
return s | factorial-trailing-zeroes | Easy Python Solution | Sneh17029 | 0 | 458 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,873 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1209051/Python3-96-Faster-Explained-with-Comments | class Solution:
def trailingZeroes(self, n: int) -> int:
'''
1. Formula: count of trailing zeros = floor(n/5)+floor(n/(5^2))+floor(n/(5^3))+floor(n/(5^4))+ ...
2. Idea: use a single while loop until floor(n/(5^i))>0 and increment count of zeros by floor(n/(5^i))
'''
cnt = 0 #initially, count of zeros is 0
i=5 #start from 5^1=5
while n//i>0:
cnt+=n//i #increment count accordingly
i*=5 #for i'th iteration, i=5^i
return cnt | factorial-trailing-zeroes | Python3 96% Faster, Explained with Comments | bPapan | 0 | 90 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,874 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1063877/python-or-faster-than-98.20 | class Solution:
def trailingZeroes(self, n: int) -> int:
cnt = 0
while n >= 5:
cnt += n // 5
n = n // 5
return cnt | factorial-trailing-zeroes | python | faster than 98.20% | nancyszy | 0 | 196 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,875 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1162331/using-math-in-python-3 | class Solution:
def trailingZeroes(self, n: int) -> int:
m=math.factorial(n)
m=list(map(int,str(m)))
m=m[::-1]
count=0
for p in m:
if p==0:
count+=1
else:
break
return(count) | factorial-trailing-zeroes | using math in python 3 | janhaviborde23 | -1 | 158 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,876 |
https://leetcode.com/problems/factorial-trailing-zeroes/discuss/1259517/Python3-simple-solution | class Solution:
def trailingZeroes(self, n: int) -> int:
x = 1
res = 0
while 5**x < 10**4+1:
res += n//5**x
x += 1
return res | factorial-trailing-zeroes | Python3 simple solution | EklavyaJoshi | -2 | 111 | factorial trailing zeroes | 172 | 0.418 | Medium | 2,877 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/1965156/Python-TC-O(1)-SC-O(h)-Generator-Solution | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.iter = self._inorder(root)
self.nxt = next(self.iter, None)
def _inorder(self, node: Optional[TreeNode]) -> Generator[int, None, None]:
if node:
yield from self._inorder(node.left)
yield node.val
yield from self._inorder(node.right)
def next(self) -> int:
res, self.nxt = self.nxt, next(self.iter, None)
return res
def hasNext(self) -> bool:
return self.nxt is not None | binary-search-tree-iterator | [Python] TC O(1) SC O(h) Generator Solution | zayne-siew | 34 | 2,300 | binary search tree iterator | 173 | 0.692 | Medium | 2,878 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/453963/Python-amortized-O(1)-sol.-on-stack-run-time-93%2B | class BSTIterator:
def __init__(self, root: TreeNode):
self.root = root
self._stack_inorder = []
self.push_left_children( root )
def push_left_children(self, node: TreeNode):
while node:
# push left child into stack
self._stack_inorder.append( node )
# update node as left child
node = node.left
def next(self) -> int:
"""
@return the next smallest number
"""
# pop next element with inorder
node = self._stack_inorder.pop()
# keep inorder collection on right subtree of node
self.push_left_children( node.right )
return node.val
def hasNext(self) -> bool:
"""
@return whether we have a next smallest number
"""
return len(self._stack_inorder) != 0 | binary-search-tree-iterator | Python amortized O(1) sol. on stack, run time 93%+ | brianchiang_tw | 2 | 203 | binary search tree iterator | 173 | 0.692 | Medium | 2,879 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/2489705/Python3-O(H)-solution-with-stacks. | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.root = root
self.stack = []
while root:
self.stack.append(root)
root = root.left
def next(self) -> int:
if self.stack:
x = self.stack[-1]
z = self.stack.pop()
if z.right:
z = z.right
while z:
self.stack.append(z)
z = z.left
return x.val
def hasNext(self) -> bool:
if self.stack:
return True
return False | binary-search-tree-iterator | Python3 O(H) solution with stacks. | vijaymurugan1457 | 1 | 31 | binary search tree iterator | 173 | 0.692 | Medium | 2,880 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/1684108/Python-solution | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.inorder = collections.deque()
def inOrderTraverse(root):
if root.left:
inOrderTraverse(root.left)
self.inorder.append(root.val)
if root.right:
inOrderTraverse(root.right)
inOrderTraverse(root)
def next(self) -> int:
return self.inorder.popleft()
def hasNext(self) -> bool:
return self.inorder | binary-search-tree-iterator | Python solution | byroncharly3 | 1 | 72 | binary search tree iterator | 173 | 0.692 | Medium | 2,881 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/1486751/Python%3A-Inorder-traversal-of-BST-is-strictly-increasing | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.nodes = [-1]
def preorder(node):
if node:
preorder(node.left)
self.nodes.append(node.val)
preorder(node.right)
preorder(root)
self.pointer = 0
self.len = len(self.nodes)
def next(self) -> int:
self.pointer += 1
return self.nodes[self.pointer]
def hasNext(self) -> bool:
return self.pointer < self.len -1 | binary-search-tree-iterator | Python: Inorder traversal of BST is strictly increasing | byuns9334 | 1 | 107 | binary search tree iterator | 173 | 0.692 | Medium | 2,882 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/2682207/Python-(Faster-than-92)-or-Using-stack | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.stack = []
while root:
self.stack.append(root)
root = root.left
def next(self) -> int:
res = self.stack.pop()
curr = res.right
while curr:
self.stack.append(curr)
curr = curr.left
return res.val
def hasNext(self) -> bool:
return len(self.stack) != 0 | binary-search-tree-iterator | Python (Faster than 92%) | Using stack | KevinJM17 | 0 | 4 | binary search tree iterator | 173 | 0.692 | Medium | 2,883 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/2650694/Python-or-Two-solutions-using-a-generator-and-a-stack | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.iterator = self.traverse(root)
self.next_node = next(self.iterator, None)
def traverse(self, node):
if node:
yield from self.traverse(node.left)
yield node.val
yield from self.traverse(node.right)
def next(self) -> int:
node, self.next_node = self.next_node, next(self.iterator, None)
return node
def hasNext(self) -> bool:
return self.next_node | binary-search-tree-iterator | Python | Two solutions using a generator and a stack | ahmadheshamzaki | 0 | 44 | binary search tree iterator | 173 | 0.692 | Medium | 2,884 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/2650694/Python-or-Two-solutions-using-a-generator-and-a-stack | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.stack = deque()
self.node = root
def next(self) -> int:
while self.node:
self.stack.append(self.node)
self.node = self.node.left
current = self.stack.pop()
self.node = current.right
return current.val
def hasNext(self) -> bool:
return self.node or self.stack | binary-search-tree-iterator | Python | Two solutions using a generator and a stack | ahmadheshamzaki | 0 | 44 | binary search tree iterator | 173 | 0.692 | Medium | 2,885 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/2638168/Python-Simple-Clean-Easy-T%3A-O(1)-S%3A-O(H) | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.stack = []
while root:
self.stack.append(root)
root = root.left
def next(self) -> int:
res = self.stack.pop()
cur = res.right
while cur:
self.stack.append(cur)
cur = cur.left
return res.val
def hasNext(self) -> bool:
return self.stack != [] | binary-search-tree-iterator | ✅ [Python] Simple, Clean, Easy T: O(1) S: O(H) | girraj_14581 | 0 | 13 | binary search tree iterator | 173 | 0.692 | Medium | 2,886 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/2477204/Simplest-95-faster | class BSTIterator(object):
def __init__(self, root):
self.stack = []
def traverse_inorder(node):
if not node:
return
traverse_inorder(node.left)
self.stack.append(node.val)
traverse_inorder(node.right)
traverse_inorder(root)
self.stack = self.stack[::-1]
def next(self):
return self.stack.pop()
def hasNext(self):
return len(self.stack) != 0 | binary-search-tree-iterator | Simplest 95% faster | Abhi_009 | 0 | 26 | binary search tree iterator | 173 | 0.692 | Medium | 2,887 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/2477204/Simplest-95-faster | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.stack = []
# storing the inorder
while root:
self.stack.append(root)
root = root.left
def next(self) -> int:
res = self.stack.pop()
cur = res.right
while cur:
self.stack.append(cur)
cur = cur.left
return res.val
def hasNext(self) -> bool:
return self.stack!=[] | binary-search-tree-iterator | Simplest 95% faster | Abhi_009 | 0 | 26 | binary search tree iterator | 173 | 0.692 | Medium | 2,888 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/2446535/Python3.-DFSRecursion.-O(n)-space-O(1)-(iterator-time)-O(n)-(preprocessing-time) | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self._values = []
self._idx = 0
self._dfs(root) # Tree traversal. O(n) time complexity
def _dfs(self, node):
if node is None: # Corner case / End of a recursion
return
# Inorder traversal part (from the leftmost to the rightmost)
self._dfs(node.left)
self._values.append(node.val)
self._dfs(node.right)
def next(self) -> int: # O(1) time complexity
res = self._values[self._idx]
self._idx += 1
return res
def hasNext(self) -> bool: # O(1) time complexity
return self._idx < len(self._values) | binary-search-tree-iterator | Python3. DFS/Recursion. O(n) space, O(1) (iterator time), O(n) (preprocessing time) | NonameDeadinside | 0 | 16 | binary search tree iterator | 173 | 0.692 | Medium | 2,889 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/2341470/Binary-Search-Tree-Iterator-(Python3) | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.stack=[]
while root:
self.stack.append(root)
root=root.left
def next(self) -> int:
res=self.stack.pop()
cur=res.right
while cur:
self.stack.append(cur)
cur=cur.left
return res.val
def hasNext(self) -> bool:
return self.stack!=[] | binary-search-tree-iterator | Binary Search Tree Iterator (Python3) | jite | 0 | 34 | binary search tree iterator | 173 | 0.692 | Medium | 2,890 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/1966984/Python-optimal-O(log-N)-stack-solution | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.stack = []
while root:
self.stack.append(root)
root = root.left
def next(self) -> int:
node = self.stack.pop()
ret_value = node.val
if node.right:
node = node.right
self.stack.append(node)
while node.left:
node = node.left
self.stack.append(node)
return ret_value
def hasNext(self) -> bool:
return bool(self.stack) | binary-search-tree-iterator | Python, optimal O(log N) stack solution | blue_sky5 | 0 | 19 | binary search tree iterator | 173 | 0.692 | Medium | 2,891 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/1966759/Python3-very-simple-in_order-deque | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.arr = deque()
self.in_order(root)
def in_order(self, root: Optional[TreeNode]):
if root:
self.in_order(root.left)
self.arr += [root.val]
self.in_order(root.right)
def next(self) -> int:
return self.arr.popleft()
def hasNext(self) -> bool:
return len(self.arr) > 0 | binary-search-tree-iterator | Python3 very simple in_order deque | Tallicia | 0 | 38 | binary search tree iterator | 173 | 0.692 | Medium | 2,892 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/1966643/Python-7-lines | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
inOrder = lambda n: inOrder(n.left) + [n.val] + inOrder(n.right) if n else []
self.vals = inOrder(root)
self.i = -1
self.end = len(self.vals) - 1
def next(self) -> int:
self.i += 1
return self.vals[self.i]
def hasNext(self) -> bool:
return self.i < self.end | binary-search-tree-iterator | Python 7 lines | SmittyWerbenjagermanjensen | 0 | 27 | binary search tree iterator | 173 | 0.692 | Medium | 2,893 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/1966011/Binary-Search-Iterator-Python.-O(h)-memory-complexity. | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.stack = []
while root:
self.stack.append(root)
root = root.left
def next(self) -> int:
res = self.stack.pop()
curr = res.right
while curr:
self.stack.append(curr)
curr = curr.left
return res.val
def hasNext(self) -> bool:
return True if self.stack else False | binary-search-tree-iterator | Binary Search Iterator Python. O(h) memory complexity. | MaverickEyedea | 0 | 27 | binary search tree iterator | 173 | 0.692 | Medium | 2,894 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/1965814/Python3-Solution-with-using-recursive-dfs | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.sorted_vals = []
self.iter = 0
self.travers(root)
def travers(self, node):
if not node:
return
self.travers(node.left)
self.sorted_vals.append(node.val)
self.travers(node.right)
def next(self) -> int:
val = self.sorted_vals[self.iter]
self.iter += 1
return val
def hasNext(self) -> bool:
return self.iter < len(self.sorted_vals) | binary-search-tree-iterator | [Python3] Solution with using recursive dfs | maosipov11 | 0 | 13 | binary search tree iterator | 173 | 0.692 | Medium | 2,895 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/1965798/java-python-simple-easy-small-(Time-On-space-On) | class BSTIterator:
def __init__(self, root: Optional[TreeNode]) :
self.node = root
self.l = []
def next(self) -> int:
while self.node != None or len(self.l) != 0 :
if self.node != None :
self.l.append(self.node)
self.node = self.node.left
else :
self.node = self.l.pop()
val = self.node.val;
self.node = self.node.right
return val
def hasNext(self) -> bool:
return self.node != None or len(self.l) != 0 | binary-search-tree-iterator | java, python - simple, easy, small (Time - On, space - On) | ZX007java | 0 | 28 | binary search tree iterator | 173 | 0.692 | Medium | 2,896 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/1965496/python3-Solution-O(1)-time-and-O(n)-space-complexity | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.root = root
self.list = [float('-inf')]
self.traverse(self.root)
self.ind = 0
def traverse(self, node):
if node:
self.traverse(node.left)
self.list.append(node.val)
self.traverse(node.right)
def next(self) -> int:
self.ind+=1
return self.list[self.ind]
def hasNext(self) -> bool:
if self.ind<len(self.list)-1:
return True
else:
return False | binary-search-tree-iterator | python3 Solution O(1) time and O(n) space complexity | shubham3 | 0 | 9 | binary search tree iterator | 173 | 0.692 | Medium | 2,897 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/1965075/Python-or-Stack-or-O(h)-memory-or-wfollow-up-or-Explained | class BSTIterator:
# finds the leftmost descendant of currentNode
def moveLeftest(self):
while self.currentNode.left:
self.nodeStack.append(self.currentNode)
self.currentNode = self.currentNode.left
def __init__(self, root: Optional[TreeNode]):
self.nodeStack = []
self.currentNode = root
# the first node in inorder traversal is the leftmost descendant of root
self.moveLeftest()
# we move one position to the left to phantom node
self.nodeStack.append(self.currentNode)
self.currentNode = TreeNode(-1)
def next(self) -> int:
# if currentNode has a right child, we move towards it, and
# then go to its leftmost descendant
if self.currentNode.right:
self.currentNode = self.currentNode.right
self.moveLeftest()
# if currentNode does not have a right child, the next node in
# inorder traversal is the back node of nodeStack
else:
self.currentNode = self.nodeStack.pop()
return self.currentNode.val
def hasNext(self) -> bool:
# there is a node to move to if currentNode has a right child
# or if nodeStack is not empty
if self.currentNode.right or self.nodeStack:
return True
return False | binary-search-tree-iterator | Python | Stack | O(h) memory | w/follow up | Explained | sr_vrd | 0 | 19 | binary search tree iterator | 173 | 0.692 | Medium | 2,898 |
https://leetcode.com/problems/binary-search-tree-iterator/discuss/1962432/Python-Inorder-Traversal-Solution-94.65-Faster | class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.aux = self.inorderTraversal(root)
def inorderTraversal(self, node):
aux = []
if not node:
return None
else:
if node.left:
aux += self.inorderTraversal(node.left)
aux += [node.val]
if node.right:
aux += self.inorderTraversal(node.right)
return aux
def next(self) -> int:
return self.aux.pop(0)
def hasNext(self) -> bool:
if self.aux:
return True
else:
return False | binary-search-tree-iterator | Python - Inorder Traversal Solution - 94.65% Faster | rmateusc | 0 | 29 | binary search tree iterator | 173 | 0.692 | Medium | 2,899 |
Subsets and Splits