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/two-sum-ii-input-array-is-sorted/discuss/2836519/Simple-python-two-pointers-solution
class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: left = 0 right = len(numbers) - 1 res = [] while left < right: sum_num = numbers[left] + numbers[right] if sum_num == target: res.append(left + 1) res.append(right + 1) return res if sum_num < target: left += 1 else: right -= 1 return res
two-sum-ii-input-array-is-sorted
Simple python two pointers solution
khaled_achech
0
1
two sum ii input array is sorted
167
0.6
Medium
2,700
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/discuss/2828986/Binary-Search-Approach-or-O(nlogn)-time-or-O(1)-space
class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: for i in range(len(numbers)): left = 0 right = len(numbers)-1 while left <= right: mid = (left + right) // 2 if numbers[mid] == target - numbers[i] and mid != i: return [i+1, mid+1] elif numbers[mid] <= target - numbers[i]: left = mid + 1 else: right = mid - 1
two-sum-ii-input-array-is-sorted
Binary Search Approach | O(nlogn) time | O(1) space
advanced-bencoding
0
3
two sum ii input array is sorted
167
0.6
Medium
2,701
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/discuss/2813472/TC%3A-81.25-Python-simple-solution
class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: l = 0 n = len(numbers) r = n-1 #find the largest minvalue of target while(l < r): if numbers[l] + numbers[r] == target: return [l+1, r+1] if numbers[l] + numbers[r] > target: r -= 1 else: l += 1 return [-1, -1]
two-sum-ii-input-array-is-sorted
😎 TC: 81.25% Python simple solution
Pragadeeshwaran_Pasupathi
0
5
two sum ii input array is sorted
167
0.6
Medium
2,702
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/discuss/2801150/easy-and-short-Python3-solution
class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: c={} for i,n in enumerate (numbers): if target - n in c: return ([c[target-n]+1,i+1]) elif n not in c: c[n] = i
two-sum-ii-input-array-is-sorted
easy and short Python3 solution
ThunderG
0
1
two sum ii input array is sorted
167
0.6
Medium
2,703
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/discuss/2782887/Two-pointers
class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: l, r = 0, len(numbers)-1 while l<r: if numbers[l]+numbers[r] == target: return [l+1,r+1] elif numbers[l]+numbers[r] > target: r -= 1 else: l +=1
two-sum-ii-input-array-is-sorted
Two pointers
haniyeka
0
1
two sum ii input array is sorted
167
0.6
Medium
2,704
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/discuss/2779861/Python-2-Pointers-Solution-Easy
class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: left = 0 right = len(numbers) - 1 while (left < right): curr_sum = numbers[left] + numbers[right] if curr_sum == target: return [left + 1, right + 1] elif numbers[left] + numbers[right] < target: left += 1 else: right -= 1
two-sum-ii-input-array-is-sorted
Python 2 Pointers Solution Easy
gpersonnat
0
3
two sum ii input array is sorted
167
0.6
Medium
2,705
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/discuss/2775669/Python3-O(n)-O(1)-Solution-with-Explanation
class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: l = 0 r = len(numbers)-1 while l < r: sum = numbers[l] + numbers[r] if sum < target: l += 1 elif sum > target: r -= 1 else: return [l+1, r+1] return []
two-sum-ii-input-array-is-sorted
Python3 O(n) / O(1) Solution with Explanation
abarrett879
0
3
two sum ii input array is sorted
167
0.6
Medium
2,706
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/discuss/2754087/LR-Pointer
class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: # start with a pointer on each end # get the sums of the two pointers # if the sum is larger than target move right # if the sum is smaller than target move left # if equals, return index of points (one added) # time O(n) space O(1) l, r = 0, len(numbers) - 1 while l < r: while numbers[l] + numbers[r] > target: r -= 1 while numbers[l] + numbers[r] < target: l += 1 if numbers[l] + numbers[r] == target: return [l + 1, r + 1]
two-sum-ii-input-array-is-sorted
LR Pointer
andrewnerdimo
0
2
two sum ii input array is sorted
167
0.6
Medium
2,707
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/discuss/2748931/python-solution-two-pointer
class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: # O(n), O(1) left = 0 right = len(numbers) - 1 while left < right: add = numbers[left] + numbers[right] if add == target: return [left + 1, right + 1] elif add > target: right -= 1 else: left += 1
two-sum-ii-input-array-is-sorted
python solution two pointer
sahilkumar158
0
5
two sum ii input array is sorted
167
0.6
Medium
2,708
https://leetcode.com/problems/excel-sheet-column-title/discuss/2448578/Easy-oror-0-ms-oror-100-oror-Fully-Explained-(Java-C%2B%2B-Python-Python3)
class Solution(object): def convertToTitle(self, columnNumber): # Create an empty string for storing the characters... output = "" # Run a while loop while columnNumber is positive... while columnNumber > 0: # Subtract 1 from columnNumber and get current character by doing modulo of columnNumber by 26... output = chr(ord('A') + (columnNumber - 1) % 26) + output # Divide columnNumber by 26... columnNumber = (columnNumber - 1) // 26 # Return the output string. return output
excel-sheet-column-title
Easy || 0 ms || 100% || Fully Explained (Java, C++, Python, Python3)
PratikSen07
22
1,300
excel sheet column title
168
0.348
Easy
2,709
https://leetcode.com/problems/excel-sheet-column-title/discuss/336155/Solution-in-Python-3-(beats-100)
class Solution: def convertToTitle(self, n: int) -> str: s = '' while n > 0: r = n%26 if r == 0: r = 26 s = chr(64+r)+s n = int((n-r)/26) return(s) - Python 3 - Junaid Mansuri
excel-sheet-column-title
Solution in Python 3 (beats 100%)
junaidmansuri
11
1,800
excel sheet column title
168
0.348
Easy
2,710
https://leetcode.com/problems/excel-sheet-column-title/discuss/1309650/Python-20-ms-faster-than-98
class Solution: def convertToTitle(self, columnNumber: int) -> str: ans = "" while columnNumber: columnNumber -= 1 ans = chr(65 + columnNumber % 26) + ans columnNumber //= 26 return ans ```
excel-sheet-column-title
Python, 20 ms, faster than 98%
MihailP
5
934
excel sheet column title
168
0.348
Easy
2,711
https://leetcode.com/problems/excel-sheet-column-title/discuss/1628320/Python-recursive-solution-oror-faster-than-94-submissions
class Solution: def convertToTitle(self, columnNumber: int) -> str: if columnNumber==0: return '' q,r=divmod(columnNumber-1,26) return self.convertToTitle(q)+chr(r+ord('A'))
excel-sheet-column-title
Python recursive solution || faster than 94% submissions
diksha_choudhary
4
346
excel sheet column title
168
0.348
Easy
2,712
https://leetcode.com/problems/excel-sheet-column-title/discuss/1003417/Python3-Faster-than-98-or-Iterative-or-Easy-to-understand
class Solution: def convertToTitle(self, n: int) -> str: ans='' while n: ans=chr(ord('A')+((n-1)%26))+ans n=(n-1)//26 return ans
excel-sheet-column-title
[Python3] Faster than 98% | Iterative | Easy to understand
_vaishalijain
3
302
excel sheet column title
168
0.348
Easy
2,713
https://leetcode.com/problems/excel-sheet-column-title/discuss/249459/Python3-beats-100
class Solution(object): def convertToTitle(self, n): ref = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" s = "" while n > 0: n = n-1 s = ref[n % 26] + s n = n // 26 return s
excel-sheet-column-title
Python3 beats 100%
vishnuvs
3
286
excel sheet column title
168
0.348
Easy
2,714
https://leetcode.com/problems/excel-sheet-column-title/discuss/1837466/Python-simple-and-elegant!
class Solution: def convertToTitle(self, n: int) -> str: ans = "" while n: n, r = (n-1)//26, (n-1)%26 ans = chr(ord("A")+r) + ans return ans
excel-sheet-column-title
Python - simple and elegant!
domthedeveloper
2
279
excel sheet column title
168
0.348
Easy
2,715
https://leetcode.com/problems/excel-sheet-column-title/discuss/1837466/Python-simple-and-elegant!
class Solution: def convertToTitle(self, n: int) -> str: ans = "" while n: n, r = divmod(n-1,26) ans = chr(ord("A")+r) + ans return ans
excel-sheet-column-title
Python - simple and elegant!
domthedeveloper
2
279
excel sheet column title
168
0.348
Easy
2,716
https://leetcode.com/problems/excel-sheet-column-title/discuss/1837466/Python-simple-and-elegant!
class Solution: def convertToTitle(self, n: int) -> str: ans = "" c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" while n: n, r = divmod(n-1,26) ans = c[r] + ans return ans
excel-sheet-column-title
Python - simple and elegant!
domthedeveloper
2
279
excel sheet column title
168
0.348
Easy
2,717
https://leetcode.com/problems/excel-sheet-column-title/discuss/1837466/Python-simple-and-elegant!
class Solution: def convertToTitle(self, n): ans = "" c = list(map(chr,range(ord("A"),ord("Z")+1))) while n: n, r = divmod(n-1,26) ans = c[r] + ans return ans
excel-sheet-column-title
Python - simple and elegant!
domthedeveloper
2
279
excel sheet column title
168
0.348
Easy
2,718
https://leetcode.com/problems/excel-sheet-column-title/discuss/2808510/Python-faster-than-99
class Solution: dicty={i:chr(65+i) for i in range(26)} def convertToTitle(self, columnNumber: int) -> str: i=0 while True: if columnNumber-26**i<0: i-=1 break columnNumber-=26**i i+=1 res="" for j in range(i,-1,-1): res=res+self.dicty[columnNumber//(26**j)] columnNumber-=26**j*(columnNumber//(26**j)) return res
excel-sheet-column-title
Python faster than 99%
timyoxam
1
208
excel sheet column title
168
0.348
Easy
2,719
https://leetcode.com/problems/excel-sheet-column-title/discuss/2108357/Python-solution
class Solution: def convertToTitle(self, num: int) -> str: s = '' while num>0: num -= 1 rightmost = chr((num%26) + ord('A')) num //= 26 s+=rightmost return s[::-1]
excel-sheet-column-title
Python solution
leadingcoder
1
116
excel sheet column title
168
0.348
Easy
2,720
https://leetcode.com/problems/excel-sheet-column-title/discuss/1588244/Python-Easy-Approach-or-Simple-Maths
class Solution: def convertToTitle(self, columnNumber: int) -> str: # AB = A×26¹+B = 1×26¹+2 # ABCD=A×26³+B×26²+C×26¹+D=1×26³+2×26²+3×26¹+4 sheet = [chr(i) for i in range(ord("A"), ord("Z")+1)] res = [] while columnNumber > 0: res.append(sheet[(columnNumber-1) % 26]) columnNumber = (columnNumber-1)//26 return "".join(res[::-1])
excel-sheet-column-title
Python Easy Approach | Simple Maths
leet_satyam
1
393
excel sheet column title
168
0.348
Easy
2,721
https://leetcode.com/problems/excel-sheet-column-title/discuss/1270089/Python3-simple-solution-using-modulo-operator
class Solution: def convertToTitle(self, columnNumber: int) -> str: res = '' alpha = {} for i in range(26): alpha[i] = chr(97+i).upper() while columnNumber > 0: columnNumber -= 1 res = alpha[columnNumber % 26] + res columnNumber //= 26 return res
excel-sheet-column-title
Python3 simple solution using modulo operator
EklavyaJoshi
1
135
excel sheet column title
168
0.348
Easy
2,722
https://leetcode.com/problems/excel-sheet-column-title/discuss/975036/Python-simple-solution
class Solution: def convertToTitle(self, n: int) -> str: result = "" num = n while(True): rem = num % 26 if rem == 0: result = 'Z' + result num = num - 1 else: result = chr(ord('A') + (rem -1)) + result num = num // 26 if num <= 0: break return result
excel-sheet-column-title
Python simple solution
shruti98932
1
312
excel sheet column title
168
0.348
Easy
2,723
https://leetcode.com/problems/excel-sheet-column-title/discuss/796946/Using-dictionary
class Solution: def convertToTitle(self, n: int) -> str: s2="" s=string.ascii_uppercase d={0:"Z"} for i in range(26): d[i+1]=s[i] while n!=0: x=n%26 n//=26 s2+=d[x] if x==0: n-=1 return s2[::-1]
excel-sheet-column-title
Using dictionary
thisisakshat
1
258
excel sheet column title
168
0.348
Easy
2,724
https://leetcode.com/problems/excel-sheet-column-title/discuss/727868/Python3-26-carry-system
class Solution: def convertToTitle(self, n: int) -> str: ans = [] while n: n, r = divmod(n-1, 26) ans.append(r) return "".join(chr(r+65) for r in reversed(ans))
excel-sheet-column-title
[Python3] 26-carry system
ye15
1
94
excel sheet column title
168
0.348
Easy
2,725
https://leetcode.com/problems/excel-sheet-column-title/discuss/650285/Python-solution
class Solution: def convertToTitle(self, n: int) -> str: letters = ["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"] result = "" while n: result = letters[(n - 1) % 26] + result n = (n - 1) // 26 return result
excel-sheet-column-title
Python solution
simplyalde
1
289
excel sheet column title
168
0.348
Easy
2,726
https://leetcode.com/problems/excel-sheet-column-title/discuss/2797511/Simple-Python3-Solution-using-While-Loop
class Solution: def convertToTitle(self, columnNumber: int) -> str: characters = [chr(x) for x in range(ord('A'), ord('Z')+1)] result = '' while columnNumber > 0: result += characters[(columnNumber-1)%26] columnNumber = (columnNumber - 1) // 26 return result[::-1]
excel-sheet-column-title
Simple Python3 Solution using While Loop
vivekrajyaguru
0
8
excel sheet column title
168
0.348
Easy
2,727
https://leetcode.com/problems/excel-sheet-column-title/discuss/2746138/Python3-Solution
class Solution: def convertToTitle(self, columnNumber: int) -> str: queue = [] while columnNumber > 0: columnNumber -= 1 rem = columnNumber % 26 columnNumber //= 26 queue.insert(0, chr(rem + 65)) return "".join(queue)
excel-sheet-column-title
Python3 Solution
paul1202
0
14
excel sheet column title
168
0.348
Easy
2,728
https://leetcode.com/problems/excel-sheet-column-title/discuss/2746036/divmod-python-solution
class Solution: def convertToTitle(self, columnNumber: int) -> str: carry = 0 ans = [] while columnNumber: columnNumber, carry = divmod(columnNumber - 1, 26) #print(chr(carry + 1 + 64)) ans.append(chr(carry + 1 + 64)) return ''.join(ans[::-1])
excel-sheet-column-title
divmod python solution
kruzhilkin
0
3
excel sheet column title
168
0.348
Easy
2,729
https://leetcode.com/problems/excel-sheet-column-title/discuss/2680443/single-line-Pythjon3-solution
class Solution: def convertToTitle(self, columnNumber: int) -> str: return self.convertToTitle((columnNumber-1)//26)+chr((columnNumber-1)%26+ord('A')) if columnNumber>26 else str(chr(columnNumber-1+ord('A')))
excel-sheet-column-title
single line Pythjon3 solution
nagi1805
0
6
excel sheet column title
168
0.348
Easy
2,730
https://leetcode.com/problems/excel-sheet-column-title/discuss/2535366/Python-Concise-and-fast-Iteration
class Solution: def convertToTitle(self, columnNumber: int) -> str: result = 0 result = "" while columnNumber: # get the residuum and the new number columnNumber, res = divmod(columnNumber-1, 26) # 65 = ord('A') result = chr(65 + res) + result return result
excel-sheet-column-title
[Python] - Concise and fast Iteration
Lucew
0
139
excel sheet column title
168
0.348
Easy
2,731
https://leetcode.com/problems/excel-sheet-column-title/discuss/2506442/Easy-Python-Solution-using-chr()-and-ord()
class Solution: def convertToTitle(self, columnNumber: int) -> str: ans=[] n=columnNumber while n>0: ch = chr(ord('A') + (n-1)%26) ans.insert(0,ch) n = (n-1)//26 return "".join(ans)
excel-sheet-column-title
Easy Python Solution [ using chr() & ord() ]
miyachan
0
80
excel sheet column title
168
0.348
Easy
2,732
https://leetcode.com/problems/excel-sheet-column-title/discuss/2349675/If-you-have-232-columns-in-Excel-I-have-bad-news-for-you.-(beats-96)
class Solution: def convertToTitle(self, columnNumber: int) -> str: if columnNumber < 27: return chr(64+columnNumber) if columnNumber < 53: return 'A' + chr(64+columnNumber-26) if columnNumber < 79: return 'B' + chr(64+columnNumber-52) if columnNumber < 105: return 'C' + chr(64+columnNumber-78) col = [] while columnNumber > 26: m = columnNumber % 26 if m == 0: col.append('Z') columnNumber = (columnNumber - 1) // 26 else: columnNumber = (columnNumber - m) // 26 col.append(chr(64+m)) if columnNumber > 0: if columnNumber == 26: col.append('Z') else: m = columnNumber % 26 col.append(chr(64+m)) return ''.join(col[::-1])
excel-sheet-column-title
If you have 2^32 columns in Excel, I have bad news for you. (beats 96%)
plzacfa
0
106
excel sheet column title
168
0.348
Easy
2,733
https://leetcode.com/problems/excel-sheet-column-title/discuss/2240372/Python3-Simple-Solution
class Solution: def convertToTitle(self, columnNumber: int) -> str: maps = { 0 : "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" } ans = '' while columnNumber > 0 : columnNumber -= 1 temp = columnNumber % 26 columnNumber = columnNumber // 26 ans += maps[temp] return ans[::-1]
excel-sheet-column-title
Python3 Simple Solution
jasoriasaksham01
0
279
excel sheet column title
168
0.348
Easy
2,734
https://leetcode.com/problems/excel-sheet-column-title/discuss/2100148/Python-one-liner
class Solution: def convertToTitle(self, columnNumber: int) -> str: return self.convertToTitle((columnNumber-1) // 26) + chr(ord('A') + (columnNumber-1) % 26) if columnNumber else ''
excel-sheet-column-title
Python one-liner
hongjiwang
0
135
excel sheet column title
168
0.348
Easy
2,735
https://leetcode.com/problems/excel-sheet-column-title/discuss/2088578/Simple-python-solution
class Solution: def convertToTitle(self, columnNumber: int) -> str: s = "" while columnNumber: remainder = (columnNumber - 1) % 26 s = chr(remainder + 65) + s columnNumber = (columnNumber - 1) // 26 return s
excel-sheet-column-title
Simple python solution
rahulsh31
0
153
excel sheet column title
168
0.348
Easy
2,736
https://leetcode.com/problems/excel-sheet-column-title/discuss/1940554/Python-solution-using-divmod
class Solution: def convertToTitle(self, columnNumber: int) -> str: LETTERS = [chr(letter) for letter in range(ord('A'), ord('Z')+1)] res = collections.deque() while columnNumber > 0: columnNumber, remainder = divmod(columnNumber - 1, 26) char = LETTERS[remainder] res.appendleft(char) return ''.join(res)
excel-sheet-column-title
Python solution using divmod
sEzio
0
204
excel sheet column title
168
0.348
Easy
2,737
https://leetcode.com/problems/excel-sheet-column-title/discuss/1880367/python3-simple%3A-using-data-dictionary-%3A-recursive-%3A-32ms
class Solution: a_to_z = {1: "A", 2: "B", 3: "C", 4: "D", 5: "E", 6: "F", 7: "G", 8: "H", 9: "I", 10: "J", 11: "K", 12: "L", 13: "M", 14: "N", 15: "O", 16: "P", 17: "Q", 18: "R", 19: "S", 20: "T", 21: "U", 22: "V", 23: "W", 24: "X", 25: "Y", 26: "Z" } def convertToTitle(self, columnNumber: int) -> str: ret_val="" #if columnNumber==0 : # return "" if columnNumber <= 26 : ret_val=Solution.a_to_z.get(columnNumber,0) #print("char:" , columnNumber) else : rem = columnNumber % 26 if rem == 0 : first=self.convertToTitle((columnNumber // 26 ) -1) second = self.convertToTitle(26) else: second=self.convertToTitle(rem) first = self.convertToTitle(columnNumber // 26) ret_val=first+second return ret_val
excel-sheet-column-title
python3 simple: using data dictionary : recursive : 32ms
kukamble
0
138
excel sheet column title
168
0.348
Easy
2,738
https://leetcode.com/problems/excel-sheet-column-title/discuss/1787047/Python3-O(1)-Space-Math
class Solution: def convertToTitle(self, columnNumber: int) -> str: num, title = columnNumber, '' while num: q, r = divmod(num - 1, 26) title = chr(65 + r) + title num = q return title
excel-sheet-column-title
Python3 O(1) Space Math
jlee9077
0
192
excel sheet column title
168
0.348
Easy
2,739
https://leetcode.com/problems/excel-sheet-column-title/discuss/1459721/Python3-Fast-and-Short-Solution-Faster-Than-93.42
class Solution: def convertToTitle(self, columnNumber: int) -> str: m, s = [chr(i) for i in range(ord('A'), ord('Z') + 1)], "" while columnNumber > 0: s += m[(columnNumber - 1) % 26] columnNumber = (columnNumber - 1) // 26 return s[::-1]
excel-sheet-column-title
Python3 Fast & Short Solution, Faster Than 93.42%
Hejita
0
150
excel sheet column title
168
0.348
Easy
2,740
https://leetcode.com/problems/excel-sheet-column-title/discuss/1352632/Python-Solution-Beats-98.63-of-Python-Submissions
class Solution: def convertToTitle(self, cNum: int) -> str: ans='' while cNum>0: rem=cNum%26 if rem==0:rem=26 ans+=chr(64+rem) cNum=cNum//26 if rem==26:cNum-=1 ans=ans[::-1] return ans
excel-sheet-column-title
Python Solution Beats 98.63% of Python Submissions
reaper_27
0
209
excel sheet column title
168
0.348
Easy
2,741
https://leetcode.com/problems/excel-sheet-column-title/discuss/1260815/Easy-Python-Solution
class Solution: def convertToTitle(self, x: int) -> str: z=dict((i,k) for i, k in enumerate(ascii_uppercase)) app="" while(x>0): x-=1 y=x%26 x=x//26 app=str(z[y])+app return app
excel-sheet-column-title
Easy Python Solution
Sneh17029
0
614
excel sheet column title
168
0.348
Easy
2,742
https://leetcode.com/problems/excel-sheet-column-title/discuss/1247344/python3-dollarolution
class Solution: def convertToTitle(self, columnNumber: int) -> str: s, i = '', 26 while columnNumber != 0: columnNumber /= (26/i) d = int(columnNumber) % 26 if d == 0: s = 'Z' + s columnNumber -= 26 else: s = chr(d + 64) + s columnNumber -= d i = 1 return(s)
excel-sheet-column-title
python3 $olution
AakRay
0
213
excel sheet column title
168
0.348
Easy
2,743
https://leetcode.com/problems/excel-sheet-column-title/discuss/418860/Accepted-Python3%3A-Using-ord()-and-chr()
class Solution(object): def convertToTitle(self, n): """ :type n: int :rtype: str """ if n // 26 == 0: return chr(n + 64) xls_title, constant = '', 65 while n > 0: letter_order, n = (n - 1) % 26, (n - 1) // 26 xls_title = ''.join((chr(letter_order + constant), xls_title)) return xls_title
excel-sheet-column-title
Accepted Python3: Using ord() and chr()
i-i
0
138
excel sheet column title
168
0.348
Easy
2,744
https://leetcode.com/problems/excel-sheet-column-title/discuss/1752080/Simple-Python-Solution
class Solution: def convertToTitle(self, columnNumber: int) -> str: column_title = "" while columnNumber > 0: columnNumber, remainder = divmod(columnNumber - 1, 26) column_title = chr(65 + remainder) + column_title return column_title
excel-sheet-column-title
Simple Python Solution
Akhilesh_Pothuri
-1
256
excel sheet column title
168
0.348
Easy
2,745
https://leetcode.com/problems/majority-element/discuss/1788112/Python-easy-solution-O(n)-or-O(1)-or-explained
class Solution: def majorityElement(self, nums: List[int]) -> int: curr, count = nums[0], 1 # curr will store the current majority element, count will store the count of the majority for i in range(1,len(nums)): count += (1 if curr == nums[i] else -1) # if i is equal to current majority, they're in same team, hence added, else one current majority and i both will be dead if not count: # if count is 0 means King is de-throwned curr = nums[i+1] if i + 1 < len(nums) else None # the next element is the new King count = 0 # starting it with 0 because we can't increment the i of the for loop, the count will be 1 in next iteration, and again the battle continues after next iteration return curr
majority-element
✅ Python easy solution O(n) | O(1) | explained
dhananjay79
18
1,700
majority element
169
0.639
Easy
2,746
https://leetcode.com/problems/majority-element/discuss/1788112/Python-easy-solution-O(n)-or-O(1)-or-explained
class Solution: def majorityElement(self, nums: List[int]) -> int: curr, count = nums[0], 1 # curr will store the current majority element, count will store the count of the majority for i in nums[1:]: count += (1 if curr == i else -1) # if i is equal to current majority, they're in same team, hence added, else one current majority and i both will be dead if not count: # if count of current majority is zero, then change the majority element, and start its count from 1 curr = i count = 1 return curr
majority-element
✅ Python easy solution O(n) | O(1) | explained
dhananjay79
18
1,700
majority element
169
0.639
Easy
2,747
https://leetcode.com/problems/majority-element/discuss/2379248/Very-Easy-100(Fully-Explained)(C%2B%2B-Java-Python-JS-C-Python3)
class Solution(object): def majorityElement(self, nums): sol = None cnt = 0 for i in nums: if cnt == 0: sol = i cnt += (1 if i == sol else -1) return sol
majority-element
Very Easy 100%(Fully Explained)(C++, Java, Python, JS, C, Python3)
PratikSen07
17
1,400
majority element
169
0.639
Easy
2,748
https://leetcode.com/problems/majority-element/discuss/2379248/Very-Easy-100(Fully-Explained)(C%2B%2B-Java-Python-JS-C-Python3)
class Solution: def majorityElement(self, nums: List[int]) -> int: sol = None cnt = 0 for i in nums: if cnt == 0: sol = i cnt += (1 if i == sol else -1) return sol
majority-element
Very Easy 100%(Fully Explained)(C++, Java, Python, JS, C, Python3)
PratikSen07
17
1,400
majority element
169
0.639
Easy
2,749
https://leetcode.com/problems/majority-element/discuss/2255256/Python-O(n)-Time-O(1)-Space-solution
class Solution: def majorityElement(self, nums): count = 0 candidate = None for num in nums: if count == 0: candidate = num count +=1 elif candidate == num: count +=1 else: count -=1 return candidate
majority-element
Python O(n) Time, O(1) Space solution
maj3r
5
284
majority element
169
0.639
Easy
2,750
https://leetcode.com/problems/majority-element/discuss/1028321/Short-and-easy-python3-solution-faster-than-98.23
class Solution: def majorityElement(self, nums: List[int]) -> int: nums.sort() return nums[len(nums)//2]
majority-element
Short and easy python3 solution faster than 98.23%
arpit0891
5
251
majority element
169
0.639
Easy
2,751
https://leetcode.com/problems/majority-element/discuss/1815592/Python-3-Counter-HashMap-or-Beats-92
class Solution: def majorityElement(self, nums: List[int]) -> int: count = Counter(nums) for key, val in count.items(): if val > len(nums)//2: return key
majority-element
[Python 3] Counter, HashMap | Beats 92%
hari19041
3
170
majority element
169
0.639
Easy
2,752
https://leetcode.com/problems/majority-element/discuss/1232011/Easy-solution-in-python-with-explanation-and-faster-than-93%2B.
class Solution: def majorityElement(self, nums: List[int]) -> int: s=list(set(nums)) l=len(nums)/2 for i in s: if nums.count(i)>l: return i
majority-element
Easy solution in python with explanation and faster than 93%+.
souravsingpardeshi
3
496
majority element
169
0.639
Easy
2,753
https://leetcode.com/problems/majority-element/discuss/1733933/Simple-Python-(Boyer-Moore-Voting-Algorithm)%3A
class Solution: def majorityElement(self, nums: List[int]) -> int: index=0 count=1 for i in range(len(nums)): if nums[index]==nums[i]: count+=1 else: count-=1 if count==0: index=i count=1 return nums[index]
majority-element
Simple Python (Boyer Moore Voting Algorithm):
goxy_coder
2
159
majority element
169
0.639
Easy
2,754
https://leetcode.com/problems/majority-element/discuss/616339/PythonJSGo-O(n)-by-BM-algorithm-w-Reference
class Solution: def majorityElement(self, nums: List[int]) -> int: majority = None vote = 0 for number in nums: if vote == 0: # Either starting or vote just go back to zero # assign current number as majority majority = number vote = 1 elif majority == number: # current number is majority, add 1 to vote vote += 1 else: # current numbr is not majority, substract 1 from vote vote -= 1 return majority
majority-element
Python/JS/Go O(n) by BM algorithm [w/ Reference]
brianchiang_tw
2
330
majority element
169
0.639
Easy
2,755
https://leetcode.com/problems/majority-element/discuss/2542960/Python3-oror-Hashmap-oror-Easy-solution
class Solution: def majorityElement(self, nums: List[int]) -> int: num=Counter(nums) max=float(-inf) for i in num.keys(): if(num[i]>max): max=num[i] res=i return res
majority-element
✔Python3 || Hashmap || Easy solution
kaii-23
1
44
majority element
169
0.639
Easy
2,756
https://leetcode.com/problems/majority-element/discuss/2093952/Python3-from-O(n)-to-O(1)-in-memory-with-and-with-hashMaps
class Solution: def majorityElement(self, nums: List[int]) -> int: # return self.majorityElementOptimal(nums) return self.majorityElementWithHashMap(nums) # O(n) || O(1) # runtime: 176ms 83.39% def majorityElementOptimal(self, nums): if not nums: return nums count = 1 majority = nums[0] for i in range(1, len(nums)): num = nums[i] if count == 0: count = 1 majority = num elif majority == num: count += 1 else: count -= 1 return majority # O(N) || O(N) # runtime: 313ms 12.52% def majorityElementWithHashMap(self, nums): if not nums: return nums hashMap = dict() n = len(nums) for i in nums: hashMap[i] = 1 + hashMap.get(i, 0) if hashMap[i] > (n//2): return i
majority-element
Python3 from O(n) to O(1) in memory; with and with hashMaps
arshergon
1
168
majority element
169
0.639
Easy
2,757
https://leetcode.com/problems/majority-element/discuss/2032851/Python%3A-from-a-maths-perspective-(84.36)
class Solution: def majorityElement(self, nums: List[int]) -> int: return int(statistics.median(nums))
majority-element
Python: from a maths perspective (84.36%)
erikheld
1
113
majority element
169
0.639
Easy
2,758
https://leetcode.com/problems/majority-element/discuss/2032851/Python%3A-from-a-maths-perspective-(84.36)
class Solution: def majorityElement(self, nums: List[int]) -> int: quotient, remainder = divmod(len(nums), 2) if remainder: return sorted(nums)[quotient] return int(sum(sorted(nums)[quotient - 1:quotient + 1]) / 2)
majority-element
Python: from a maths perspective (84.36%)
erikheld
1
113
majority element
169
0.639
Easy
2,759
https://leetcode.com/problems/majority-element/discuss/1819650/Single-line-solution-in-python
class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: int """ return sorted(dict(collections.Counter(nums)).items(),key=operator.itemgetter(1),reverse=True)[0][0]
majority-element
Single line solution in python
amannarayansingh10
1
98
majority element
169
0.639
Easy
2,760
https://leetcode.com/problems/majority-element/discuss/1789324/Python3-Counter-Dictionary
class Solution: def majorityElement(self, nums: List[int]) -> int: c = Counter(nums) for i in c: if c[i] > (len(nums)/2): n = i return n
majority-element
Python3 Counter Dictionary
rakin54
1
83
majority element
169
0.639
Easy
2,761
https://leetcode.com/problems/majority-element/discuss/1754777/2-Java-Solutions-and-2-Python-Solutions
class Solution: def majorityElement(self, nums: List[int]) -> int: occ = Counter(nums) return max(occ, key = lambda x: occ[x])
majority-element
2 Java Solutions and 2 Python Solutions
Pootato
1
71
majority element
169
0.639
Easy
2,762
https://leetcode.com/problems/majority-element/discuss/1754777/2-Java-Solutions-and-2-Python-Solutions
class Solution: def majorityElement(self, nums: List[int]) -> int: return max(nums, key = lambda x: nums.count(x))
majority-element
2 Java Solutions and 2 Python Solutions
Pootato
1
71
majority element
169
0.639
Easy
2,763
https://leetcode.com/problems/majority-element/discuss/1700752/Moore's-Voting-Algorithm-in-O(1)-space-oror-Fast-oror-Simple-oror-Easy-oror-Explained
class Solution: def majorityElement(self, nums: List[int]) -> int: count = 0 # Track cur count candidate = 0 # current candidate for i in nums: if count == 0: # If count is zero, make cur element as candidate candidate = i count += 1 elif i == candidate: # If cur element is same as candidate then increase count else decrease count count += 1 else: count -= 1 return candidate
majority-element
Moore's Voting Algorithm in O(1) space || Fast || Simple || Easy || Explained
shubh_leetcode
1
178
majority element
169
0.639
Easy
2,764
https://leetcode.com/problems/majority-element/discuss/1628332/Python-easy-4-lines-of-code-oror-faster-than-70
class Solution: def majorityElement(self, nums: List[int]) -> int: dic=Counter(nums) for key,value in dic.items(): if value>len(nums)//2: return key return 0
majority-element
Python easy 4 lines of code || faster than 70%
diksha_choudhary
1
150
majority element
169
0.639
Easy
2,765
https://leetcode.com/problems/majority-element/discuss/1446194/Faster-than-90-in-speed-using-Python
class Solution(object): def majorityElement(self, nums): for i in set(nums): if nums.count(i) > len(nums) // 2: return i
majority-element
Faster than 90% in speed using Python
ebrahim007
1
232
majority element
169
0.639
Easy
2,766
https://leetcode.com/problems/majority-element/discuss/1336306/WEEB-DOES-PYTHON-(3-METHODS)
class Solution: def majorityElement(self, nums: List[int]) -> int: # Method 1 return Counter(nums).most_common(1)[0][0] # Method 2 # nums.sort() # return nums[len(nums)//2] # Method 3 # memo = {} # nums = sorted(nums) # for i in range(len(nums)): # if nums[i] not in memo: memo[nums[i]] = 1 # else: memo[nums[i]] += 1 # if i > len(nums) // 2: return nums[i] # if memo[nums[i]] > len(nums) // 2: return nums[i]
majority-element
WEEB DOES PYTHON (3 METHODS)
Skywalker5423
1
223
majority element
169
0.639
Easy
2,767
https://leetcode.com/problems/majority-element/discuss/1300926/Python-quickselect-TLE
class Solution: def majorityElement(self, nums: List[int]) -> int: def partition(lo, hi): for i in range(lo, hi): if nums[i] <= nums[hi]: nums[lo], nums[i] = nums[i], nums[lo] lo += 1 nums[hi], nums[lo] = nums[lo], nums[hi] return lo def quickSelect(lo, hi): pivotIndex = partition(lo, hi) if pivotIndex == len(nums)//2: return nums[pivotIndex] if pivotIndex > len(nums)//2: return quickSelect(lo, pivotIndex-1) else: return quickSelect(pivotIndex+1, hi) return quickSelect(0, len(nums)-1)
majority-element
Python quickselect TLE?
peolsae
1
73
majority element
169
0.639
Easy
2,768
https://leetcode.com/problems/majority-element/discuss/616886/Python3-boyer-moore
class Solution: def majorityElement(self, nums: List[int]) -> int: """Boyer-Moore majority voting algo""" ans = vote = 0 for x in nums: if vote == 0: ans = x if x == ans: vote += 1 else: vote -= 1 return ans
majority-element
[Python3] boyer-moore
ye15
1
90
majority element
169
0.639
Easy
2,769
https://leetcode.com/problems/majority-element/discuss/616886/Python3-boyer-moore
class Solution: def majorityElement(self, nums: List[int]) -> int: ans, vote = None, 0 for x in nums: if ans == x: vote += 1 elif vote == 0: ans, vote = x, 1 # reset else: vote -= 1 return ans
majority-element
[Python3] boyer-moore
ye15
1
90
majority element
169
0.639
Easy
2,770
https://leetcode.com/problems/majority-element/discuss/616886/Python3-boyer-moore
class Solution: def majorityElement(self, nums: List[int]) -> int: nums.sort() return nums[len(nums)//2]
majority-element
[Python3] boyer-moore
ye15
1
90
majority element
169
0.639
Easy
2,771
https://leetcode.com/problems/majority-element/discuss/616886/Python3-boyer-moore
class Solution: def majorityElement(self, nums: List[int]) -> int: freq = dict() ans = None for x in nums: freq[x] = 1 + freq.get(x, 0) if ans is None or freq[ans] < freq[x]: ans = x return ans
majority-element
[Python3] boyer-moore
ye15
1
90
majority element
169
0.639
Easy
2,772
https://leetcode.com/problems/majority-element/discuss/337076/Solution-in-Python-3
class Solution: def majorityElement(self, nums: List[int]) -> int: L, d = len(nums), {} C = [0]*L for i in range(L): n = nums[i] if n not in d: d[n] = i else: C[d[n]] += 1 return nums[C.index(max(C))] - Python 3 - Junaid Mansuri
majority-element
Solution in Python 3
junaidmansuri
1
1,300
majority element
169
0.639
Easy
2,773
https://leetcode.com/problems/majority-element/discuss/2844685/Python-easy-solution-using-set
class Solution: def majorityElement(self, nums: List[int]) -> int: s=set(nums) l=len(nums) a=l//2 for i in s: if nums.count(i)>a: return i
majority-element
Python easy solution using set
codersplAmbuj
0
2
majority element
169
0.639
Easy
2,774
https://leetcode.com/problems/majority-element/discuss/2839397/Python-easy-solution
class Solution: def majorityElement(self, nums: List[int]) -> int: return mode(nums)
majority-element
Python easy solution
riad5089
0
3
majority element
169
0.639
Easy
2,775
https://leetcode.com/problems/majority-element/discuss/2839066/Simple-Python-Solution
class Solution: def majorityElement(self, nums: List[int]) -> int: freq={} for i in nums: if i in freq: freq[i]+=1 else: freq[i]=1 for i in freq: if freq[i]>= len(nums)/2: return i
majority-element
Simple Python Solution
sonikathan2001
0
2
majority element
169
0.639
Easy
2,776
https://leetcode.com/problems/majority-element/discuss/2839013/two-lines-....
class Solution: def majorityElement(self, nums: List[int]) -> int: nums.sort() return nums[len(nums)//2]
majority-element
two lines ....
xiaolaotou
0
1
majority element
169
0.639
Easy
2,777
https://leetcode.com/problems/majority-element/discuss/2835164/python3-solution
class Solution: def majorityElement(self, nums: List[int]) -> int: l=list(set(nums)) for x in range(len(l)): a=nums.count(l[x]) if(a>len(nums)/2): return l[x]
majority-element
python3 solution
VIKASHVAR_R
0
3
majority element
169
0.639
Easy
2,778
https://leetcode.com/problems/majority-element/discuss/2832210/2-lines-of-code-in-python-faster-than-98
class Solution: def majorityElement(self, nums: List[int]) -> int: nums=sorted(nums) return nums[len(nums)//2]
majority-element
2 lines of code in python , faster than 98%
nandhinimanickam
0
2
majority element
169
0.639
Easy
2,779
https://leetcode.com/problems/majority-element/discuss/2831231/Python3-5-lines-of-code
class Solution: def majorityElement(self, nums: List[int]) -> int: hashmap=Counter(nums) n=len(nums) for i in hashmap: if hashmap[i]>n//2: return i
majority-element
[Python3] 5 lines of code
zakaria_eljaafari
0
2
majority element
169
0.639
Easy
2,780
https://leetcode.com/problems/majority-element/discuss/2829096/EASY-Hashmap!-88-faster!-Python3!
class Solution: def majorityElement(self, nums: List[int]) -> int: log = {} nums.sort() for i in range(0, len(nums)): if nums[i] in log: log[nums[i]] = log[nums[i]] + 1 else: log[nums[i]] = 1 max_key = max(log, key=log.get) return max_key
majority-element
EASY Hashmap! 88% faster! Python3!
caiomauro
0
6
majority element
169
0.639
Easy
2,781
https://leetcode.com/problems/majority-element/discuss/2828724/Python-Solution
class Solution: def majorityElement(self, nums: List[int]) -> int: hmap = {} for char in nums: if char in hmap: hmap[char] += 1 else: hmap[char] = 1 return max(hmap, key=hmap.get)
majority-element
Python Solution
Antoine703
0
1
majority element
169
0.639
Easy
2,782
https://leetcode.com/problems/majority-element/discuss/2817682/Python-Easy-Solution
class Solution: def majorityElement(self, nums: List[int]) -> int: #Taking first element for initialization ele = nums[0] #Counting the occurence of first element. cnt = nums.count(nums[0]) #Taking set of "nums" to reduced the number of elements s = set(nums) for i in s: k = nums.count(i) if k>cnt: #For every valid case we will update the count and ele cnt = k ele = i return ele
majority-element
[Python] Easy Solution
M3Sachin
0
1
majority element
169
0.639
Easy
2,783
https://leetcode.com/problems/majority-element/discuss/2806426/Python-beats-95
class Solution: def majorityElement(self, nums: List[int]) -> int: for i in set(nums): if nums.count(i) > len(nums)/2: return i
majority-element
Python beats 95 %
DubanaevaElnura
0
3
majority element
169
0.639
Easy
2,784
https://leetcode.com/problems/majority-element/discuss/2802547/Python-or-Simple-or-Using-Moore's-Voting-Algorithm
class Solution: def majorityElement(self, nums: List[int]) -> int: majority_element, count_majority_element = 0, 0 for i in nums: if count_majority_element == 0: majority_element = i if i == majority_element: count_majority_element += 1 else: count_majority_element -= 1 return majority_element
majority-element
Python | Simple | Using Moore’s Voting Algorithm
pawangupta
0
2
majority element
169
0.639
Easy
2,785
https://leetcode.com/problems/majority-element/discuss/2787814/Easy-Python-Solution
class Solution: def majorityElement(self, nums: List[int]) -> int: from collections import Counter cl = Counter(nums) #print(cl) max = -1 for i in cl: if cl[i] > max: max = cl[i] ind = i return ind
majority-element
Easy Python Solution
fatin_istiaq
0
3
majority element
169
0.639
Easy
2,786
https://leetcode.com/problems/majority-element/discuss/2786729/Very-easy-python-dictionary-solution
class Solution: def majorityElement(self, nums: List[int]) -> int: dic = {} for i in nums: if i not in dic: dic[i] = 1 else: dic[i] += 1 for key, val in dic.items(): if val >= len(nums) / 2: return key
majority-element
Very easy python dictionary solution
inusyd
0
1
majority element
169
0.639
Easy
2,787
https://leetcode.com/problems/majority-element/discuss/2781247/Simple-Approach
class Solution: def majorityElement(self, nums: List[int]) -> int: d={} for i in nums: if i in d: d[i]+=1 else: d[i]=1 return(max(d, key=d.get))
majority-element
Simple Approach
padmavathidevi71625
0
3
majority element
169
0.639
Easy
2,788
https://leetcode.com/problems/majority-element/discuss/2780331/Using-dict
class Solution: def majorityElement(self, nums: List[int]) -> int: occr = {} for i in nums: occr[i] = occr.get(i,0) + 1 return max(occr,key = occr.get)
majority-element
Using dict
MayuD
0
1
majority element
169
0.639
Easy
2,789
https://leetcode.com/problems/majority-element/discuss/2776856/Easy-and-Fast-Python-Solution-(Moore's)
class Solution: def majorityElement(self, nums: List[int]) -> int: count = 0 vote = nums[0] for num in nums: if count == 0: vote = num count = 1 else: if num == vote: count += 1 else: count -= 1 return vote
majority-element
✅ Easy and Fast Python Solution (Moore's)
scissor
0
3
majority element
169
0.639
Easy
2,790
https://leetcode.com/problems/majority-element/discuss/2775317/easy-solution
class Solution: def majorityElement(self, nums: List[int]) -> int: s = Counter(nums).most_common(1) return s[0][0]
majority-element
easy solution
user6046z
0
4
majority element
169
0.639
Easy
2,791
https://leetcode.com/problems/majority-element/discuss/2774420/Easy-python-solution-using-dictionary
class Solution: def majorityElement(self, nums: List[int]) -> int: n = len(nums) d = {} for i in nums: d[i] = d.get(i, 0) + 1 for x, v in d.items(): if v>n/2: return x
majority-element
Easy python solution using dictionary
_debanjan_10
0
2
majority element
169
0.639
Easy
2,792
https://leetcode.com/problems/excel-sheet-column-number/discuss/1790567/Python3-CLEAN-SOLUTION-()-Explained
class Solution: def titleToNumber(self, columnTitle: str) -> int: ans, pos = 0, 0 for letter in reversed(columnTitle): digit = ord(letter)-64 ans += digit * 26**pos pos += 1 return ans
excel-sheet-column-number
✔️ [Python3] CLEAN SOLUTION (๑❛ꆚ❛๑), Explained
artod
105
8,500
excel sheet column number
171
0.614
Easy
2,793
https://leetcode.com/problems/excel-sheet-column-number/discuss/1790803/Python-easy-one-liner-or-Explaination
class Solution: def titleToNumber(self, columnTitle: str) -> int: ans = 0 for i in columnTitle: ans = ans * 26 + ord(i) - ord('A') + 1 return ans
excel-sheet-column-number
✅ Python easy one liner | Explaination
dhananjay79
16
1,000
excel sheet column number
171
0.614
Easy
2,794
https://leetcode.com/problems/excel-sheet-column-number/discuss/1790803/Python-easy-one-liner-or-Explaination
class Solution: def titleToNumber(self, title: str) -> int: return reduce(lambda x,y: x * 26 + y, map(lambda x: ord(x)-ord('A')+1,title))
excel-sheet-column-number
✅ Python easy one liner | Explaination
dhananjay79
16
1,000
excel sheet column number
171
0.614
Easy
2,795
https://leetcode.com/problems/excel-sheet-column-number/discuss/1327433/Python-3-%3A-Super-Easy-To-Understand-With-Explanation
class Solution: def titleToNumber(self, columnTitle: str) -> int: p = 1 summ = 0 for i in columnTitle[::-1] : summ += p*(ord(i)-64) # -ord(A)+1 p*= 26 return summ
excel-sheet-column-number
Python 3 : Super Easy To Understand With Explanation
rohitkhairnar
13
793
excel sheet column number
171
0.614
Easy
2,796
https://leetcode.com/problems/excel-sheet-column-number/discuss/1616047/Python-Intuitive-Straightforward-Beginner-friendly-Mathematical-Solution-Using-Dictionary
class Solution: def titleToNumber(self, columnTitle: str) -> int: # Use dictionary to map alphabets A to Z one-on-one to integer 1 to 26: alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' nums = range(1,27) alpha_to_nums = {alphabet[i]: nums[i] for i in range(len(alphabet))} # Express the formula above in a simple for loop, iterating from the last digit: column_number = 0 for pos, letter in enumerate (reversed(columnTitle)): column_number += 26**(pos) * alpha_to_nums[letter] return column_number
excel-sheet-column-number
[Python] Intuitive Straightforward Beginner-friendly Mathematical Solution Using Dictionary
ziaiz-zythoniz
7
272
excel sheet column number
171
0.614
Easy
2,797
https://leetcode.com/problems/excel-sheet-column-number/discuss/2448737/Easy-oror-100-oror-Explained-oror-Java-oror-C%2B%2Boror-Python-(1-Line)-oror-C-oror-Python3
class Solution(object): def titleToNumber(self, columnTitle): s = columnTitle[::-1] return sum([(ord(s[i]) - 64) * (26 ** i) for i in range(len(s))])
excel-sheet-column-number
Easy || 100% || Explained || Java || C++|| Python (1 Line) || C || Python3
PratikSen07
6
394
excel sheet column number
171
0.614
Easy
2,798
https://leetcode.com/problems/excel-sheet-column-number/discuss/2448737/Easy-oror-100-oror-Explained-oror-Java-oror-C%2B%2Boror-Python-(1-Line)-oror-C-oror-Python3
class Solution: def titleToNumber(self, columnTitle: str) -> int: s = columnTitle[::-1] return sum([(ord(s[i]) - 64) * (26 ** i) for i in range(len(s))])
excel-sheet-column-number
Easy || 100% || Explained || Java || C++|| Python (1 Line) || C || Python3
PratikSen07
6
394
excel sheet column number
171
0.614
Easy
2,799