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/third-maximum-number/discuss/2812987/Python-Solution-using-Try-and-Except
class Solution: def thirdMax(self, nums: List[int]) -> int: num_sort=sorted(set(nums),reverse=True) try: return num_sort[2] except: return num_sort[0]
third-maximum-number
Python Solution using Try and Except
suyog_097
0
2
third maximum number
414
0.326
Easy
7,300
https://leetcode.com/problems/third-maximum-number/discuss/2811850/Python-Elegant-Solution
class Solution: def thirdMax(self, nums: List[int]) -> int: s = set(nums) l = list(s) if len(l)<3: return max(l) return sorted(l)[-3]
third-maximum-number
Python Elegant Solution
trickycat10
0
2
third maximum number
414
0.326
Easy
7,301
https://leetcode.com/problems/third-maximum-number/discuss/2807403/solution
class Solution: def thirdMax(self, nums: List[int]) -> int: nums.sort() l = len(nums)-1 for i in range(len(nums)-1,-1,-1): if len(nums)==1: break if nums[l] == nums[l-1]: nums.pop(i) l-=1 else: l-=1 if(len(nums)>2): return nums[-3] else: return max(nums)
third-maximum-number
solution
khanismail_1
0
2
third maximum number
414
0.326
Easy
7,302
https://leetcode.com/problems/third-maximum-number/discuss/2805729/Easy-solution-in-python-beats-99.1
class Solution: def thirdMax(self, nums: List[int]) -> int: maxi_1=-9999 maxi_2=-9999 maxi_3=-2147483648 l=list(set(nums)) if len(l)>=3: for i in l: if i>maxi_1: maxi_1=i l.remove(maxi_1) for i in l: if i >maxi_2: maxi_2=i l.remove(maxi_2) for i in l: if i>maxi_3: maxi_3=i return maxi_3 if len(l)<=2: return max(l)
third-maximum-number
Easy solution in python beats 99.1%
kmpravin5
0
3
third maximum number
414
0.326
Easy
7,303
https://leetcode.com/problems/third-maximum-number/discuss/2803385/python-solution-easy-to-understand
class Solution: def thirdMax(self, nums: List[int]) -> int: distinctnums = [] currentnum = None nums.sort(reverse = True) for i in nums: if i != currentnum: distinctnums.append(i) currentnum = i try: return distinctnums[2] except: return distinctnums[0]
third-maximum-number
python solution easy to understand
muge_zhang
0
4
third maximum number
414
0.326
Easy
7,304
https://leetcode.com/problems/third-maximum-number/discuss/2781240/python3-userfriendly-code
class Solution: def thirdMax(self, nums: List[int]) -> int: nums=set(nums) nums=list(nums) nums.sort() if len(nums)>2: return nums[-3] else: return nums[-1]
third-maximum-number
python3 userfriendly code
s_ahith-
0
2
third maximum number
414
0.326
Easy
7,305
https://leetcode.com/problems/third-maximum-number/discuss/2778590/Python-Fastest-Solution-100-Runtime
class Solution: def thirdMax(self, nums: List[int]) -> int: first = second = third = None for num in nums: if(num in [first, second, third]): continue if(first is None or num > first): third = second second = first first = num elif(second is None or num > second): third = second second = num elif(third is None or num > third): third = num return third if third is not None else first
third-maximum-number
Python Fastest Solution 100% Runtime
darsigangothri0698
0
3
third maximum number
414
0.326
Easy
7,306
https://leetcode.com/problems/third-maximum-number/discuss/2752714/Python-1-line-code
class Solution: def thirdMax(self, nums: List[int]) -> int: return [sorted(set(nums))[-x] for x in range(1,len(set(nums))+1) if len(set(nums))<3 or x==3][0]
third-maximum-number
Python 1 line code
kumar_anand05
0
4
third maximum number
414
0.326
Easy
7,307
https://leetcode.com/problems/third-maximum-number/discuss/2743283/A-little-cheaty-but-it's-O(n)-time-and-O(1)-space-in-Python3
class Solution: def thirdMax(self, nums: List[int]) -> int: maxes = [-inf] * 3 for x in nums: if x > maxes[0] and x != maxes[1] and x != maxes[2]: maxes[0] = x maxes.sort() if maxes[0] != -inf: return maxes[0] else: return maxes[2]
third-maximum-number
A little cheaty, but it's O(n) time and O(1) space in Python3
ThatTallProgrammer
0
4
third maximum number
414
0.326
Easy
7,308
https://leetcode.com/problems/third-maximum-number/discuss/2738617/Simple-Python-Solution
class Solution: def thirdMax(self, nums: List[int]) -> int: list1 = sorted(set(nums)) if len(list1)>=3: b = list1[-3] return b else: c = list1[-1] return c
third-maximum-number
Simple Python Solution
dnvavinash
0
9
third maximum number
414
0.326
Easy
7,309
https://leetcode.com/problems/third-maximum-number/discuss/2736611/third-maximum-number-py3
class Solution: def thirdMax(self, nums: List[int]) -> int: if len(nums) < 3 : return max(nums) mapp = {} for val in nums : mapp[val] = 1+mapp.get(val , 0 ) if len(mapp) <= 2 : return max(nums) nums = [i for i in nums if i!=max(nums)] nums = [i for i in nums if i!=max(nums)] return max(nums)
third-maximum-number
third-maximum-number-py3
Akshpreet
0
3
third maximum number
414
0.326
Easy
7,310
https://leetcode.com/problems/third-maximum-number/discuss/2729741/Easy-and-Simple-Python-Solution
class Solution: def thirdMax(self, nums: List[int]) -> int: unique = list(set(nums)) unique.sort() return unique[-3] if len(unique) >= 3 else max(nums)
third-maximum-number
Easy and Simple Python Solution
dayaniravi123
0
3
third maximum number
414
0.326
Easy
7,311
https://leetcode.com/problems/third-maximum-number/discuss/2722394/easy-solution
class Solution: def thirdMax(self, nums: List[int]) -> int: m1=max(nums) l1=list(set(nums)) if len(l1)<3: return m1 l1.remove(m1) m2=max(l1) l1.remove(m2) m3=max(l1) return m3
third-maximum-number
easy solution
sindhu_300
0
3
third maximum number
414
0.326
Easy
7,312
https://leetcode.com/problems/third-maximum-number/discuss/2685839/easiest-python-solution
class Solution: def thirdMax(self, nums: List[int]) -> int: nums=set(nums) if len(nums)<3: return max(nums) nums.remove(max(nums)) nums.remove(max(nums)) return max(nums)
third-maximum-number
easiest python solution
sahityasetu1996
0
1
third maximum number
414
0.326
Easy
7,313
https://leetcode.com/problems/third-maximum-number/discuss/2674129/Approach-using-MaxHeap-and-Set-with-Comments.
class Solution: def thirdMax(self, nums: List[int]) -> int: nums = set(nums) if len(nums) < 3: return max(nums) else: max_heap = [] for i in nums: heapq.heappush(max_heap,-i) #Add value to heap. Since it is natively a min heap, adding a negative value turns it into a max heap. #To get third max, remove first two max numbers. heapq.heappop(max_heap) #Remove greatest value heapq.heappop(max_heap) #Remove greatest value return -max_heap[0]#Third greatest value in max heap Make it un-negative.
third-maximum-number
Approach using MaxHeap and Set with Comments.
mephiticfire
0
2
third maximum number
414
0.326
Easy
7,314
https://leetcode.com/problems/third-maximum-number/discuss/2641071/Python-Solution-or-Brute-Force
class Solution: def thirdMax(self, nums: List[int]) -> int: nums.sort() # print(nums) d={} ans=[] for num in nums: if num not in d: d[num]=1 ans.append(num) if len(ans)>2: return ans[-3] return ans[-1] <!-- class Solution: def thirdMax(self, nums: List[int]) -> int: s=sorted(list(set(nums))) if len(s)>2: return s[-3] return s[-1] -->
third-maximum-number
Python Solution | Brute Force
Siddharth_singh
0
5
third maximum number
414
0.326
Easy
7,315
https://leetcode.com/problems/third-maximum-number/discuss/2616428/oror-python-solution-oror
class Solution: def thirdMax(self, nums: List[int]) -> int: nums=sorted(set(nums), reverse=True) l=len(nums) if l>=3: return nums[2] else: return max(nums)
third-maximum-number
|| python solution ||
Shreya_sg_283
0
15
third maximum number
414
0.326
Easy
7,316
https://leetcode.com/problems/third-maximum-number/discuss/2460128/PYTHON-Simple-Straightforward-Solution-Feedbacks-are-appreciated
class Solution: def thirdMax(self, nums: List[int]) -> int: array = sorted(list(set(nums)), reverse=True) return array[2] if len(array)>2 else max(nums)
third-maximum-number
[PYTHON] Simple Straightforward Solution - Feedbacks are appreciated
Eli47
0
54
third maximum number
414
0.326
Easy
7,317
https://leetcode.com/problems/third-maximum-number/discuss/2398317/Solution-in-Python-O(nlog(n))-beats-97.95
class Solution: def thirdMax(self, nums: List[int]) -> int: ram = list(set(nums)) if len(ram)>=3: ram.sort(reverse=True) return ram[2] else: return max(ram)
third-maximum-number
Solution in Python O(nlog(n)) beats 97.95%
YangJenHao
0
93
third maximum number
414
0.326
Easy
7,318
https://leetcode.com/problems/third-maximum-number/discuss/2343130/Python3-Easy-Bucket-sort-O(n)
class Solution: def thirdMax(self, nums: List[int]) -> int: minVal = float("-inf") f, s, t = [minVal, minVal, minVal] for num in nums: if num == f or num == s or num == t: continue if num > f: t = s s = f f = num elif num > s: t = s s = num elif num > t: t = num return t if t != minVal else f
third-maximum-number
Python3 Easy Bucket sort O(n)
neth_37
0
57
third maximum number
414
0.326
Easy
7,319
https://leetcode.com/problems/third-maximum-number/discuss/2307460/python3-Easiest-solution
class Solution: def thirdMax(self, nums: List[int]) -> int: nums = set(nums) if len(nums) < 3: return max(nums) nums = list(nums) nums.remove(max(nums)) nums.remove(max(nums)) return max(nums)
third-maximum-number
python3 Easiest solution
Harshit_twist
0
34
third maximum number
414
0.326
Easy
7,320
https://leetcode.com/problems/third-maximum-number/discuss/2147943/Python-Solution-oror-simple-and-clean-code
class Solution: def thirdMax(self, nums: List[int]) -> int: l=[] for i in nums: if i not in l: l.append(i) l.sort() print(l) n=len(l) if n<=2: return max(l) else: return l[len(l)-3]
third-maximum-number
Python Solution || simple and clean code
T1n1_B0x1
0
62
third maximum number
414
0.326
Easy
7,321
https://leetcode.com/problems/third-maximum-number/discuss/2134004/python-min-heap-solution
class Solution: def thirdMax(self, nums: List[int]) -> int: heap = [] for i in set(nums): if len(heap) < 3: heapq.heappush(heap, i) else: heapq.heappushpop(heap, i) return heap[0] if len(heap) == 3 else max(heap)
third-maximum-number
python min-heap solution
writemeom
0
50
third maximum number
414
0.326
Easy
7,322
https://leetcode.com/problems/third-maximum-number/discuss/1969173/Python-3-Solution-Using-Set-and-Sorted
class Solution: def thirdMax(self, nums: List[int]) -> int: if len(sorted(list(set(nums)))) < 3: return max(nums) return sorted(list(set(nums)))[-3]
third-maximum-number
Python 3 Solution Using Set and Sorted
AprDev2011
0
53
third maximum number
414
0.326
Easy
7,323
https://leetcode.com/problems/third-maximum-number/discuss/1930488/Python-One-Liner-or-Multiple-Solutions-or-Clean-and-Simple!
class Solution: def thirdMax(self, nums): nums = sorted(set(nums), reverse=True) return nums[2] if len(nums) >= 3 else nums[0]
third-maximum-number
Python - One Liner | Multiple Solutions | Clean and Simple!
domthedeveloper
0
120
third maximum number
414
0.326
Easy
7,324
https://leetcode.com/problems/third-maximum-number/discuss/1930488/Python-One-Liner-or-Multiple-Solutions-or-Clean-and-Simple!
class Solution: def thirdMax(self, nums): return (lambda n : n[2] if len(n) >= 3 else n[0])(sorted(set(nums), reverse=True))
third-maximum-number
Python - One Liner | Multiple Solutions | Clean and Simple!
domthedeveloper
0
120
third maximum number
414
0.326
Easy
7,325
https://leetcode.com/problems/third-maximum-number/discuss/1930488/Python-One-Liner-or-Multiple-Solutions-or-Clean-and-Simple!
class Solution: def thirdMax(self, nums): m = [None, None, None] for n in set(nums): if m[0] is None or n > m[0]: m = [n, m[0], m[1]] elif m[1] is None or n > m[1]: m = [m[0], n, m[1]] elif m[2] is None or n > m[2]: m = [m[0], m[1], n] return m[2] if m[2] is not None else m[0]
third-maximum-number
Python - One Liner | Multiple Solutions | Clean and Simple!
domthedeveloper
0
120
third maximum number
414
0.326
Easy
7,326
https://leetcode.com/problems/third-maximum-number/discuss/1930488/Python-One-Liner-or-Multiple-Solutions-or-Clean-and-Simple!
class Solution: def thirdMax(self, nums): m = [-inf, -inf, -inf] for n in set(nums): if n > m[0]: m = [n, m[0], m[1]] elif n > m[1]: m = [m[0], n, m[1]] elif n > m[2]: m = [m[0], m[1], n] return m[2] if m[2] != -inf else m[0]
third-maximum-number
Python - One Liner | Multiple Solutions | Clean and Simple!
domthedeveloper
0
120
third maximum number
414
0.326
Easy
7,327
https://leetcode.com/problems/third-maximum-number/discuss/1927629/Third-Maximum-Number
class Solution: def thirdMax(self, nums: List[int]) -> int: # first we take the lenght of arr lenght = len(nums) # then we check that lenght is equal to 1 or not if (lenght == 1): return (nums[0]) # then we sort the arr in decending oder arr = sorted(nums,reverse = True) count = 1 for i in range(lenght - 1): if (arr[i] != arr[i+1]): count += 1 if (count == 3): return arr[i+1] if count < 3: return arr[0]
third-maximum-number
Third Maximum Number
shubham_pcs2012
0
43
third maximum number
414
0.326
Easy
7,328
https://leetcode.com/problems/third-maximum-number/discuss/1925091/Python-Easy-to-understand
class Solution: def thirdMax(self, nums: List[int]) -> int: firstmax = max(nums) if len(nums)<3: return firstmax else: secondmax = -9999999999999999999999 for a in nums: if secondmax < a and a != firstmax: secondmax = a nums = sorted(nums, reverse=True) for a in range(2, len(nums)): if nums[a] != firstmax and nums[a]!= secondmax: thirdmax = nums[a] return thirdmax return firstmax
third-maximum-number
Python Easy to understand
Tonmoy-saha18
0
29
third maximum number
414
0.326
Easy
7,329
https://leetcode.com/problems/third-maximum-number/discuss/1853059/Python-(Simple-Approach-and-Beginner-friendly)
class Solution: def thirdMax(self, nums: List[int]) -> int: maxi = [] count = 0 nums.sort(reverse = True) for i in nums: if i not in maxi and count<3: maxi.append(i) count+=1 if i in maxi: continue return maxi[-1] if count == 3 else max(maxi)
third-maximum-number
Python (Simple Approach and Beginner-friendly)
vishvavariya
0
90
third maximum number
414
0.326
Easy
7,330
https://leetcode.com/problems/third-maximum-number/discuss/1840412/Easy-Py3-sol
class Solution: def thirdMax(self, nums: List[int]) -> int: n=len(nums) if n<3: return max(nums) a=b=c=min(nums) for i in nums: if i>a: c=b b=a a=i if i<a and i>b: c=b b=i if i<b and i>c: c=i if b!=c: return c return max(nums)
third-maximum-number
Easy Py3 sol
Xhubham
0
43
third maximum number
414
0.326
Easy
7,331
https://leetcode.com/problems/third-maximum-number/discuss/1653355/Python-Using-Heapq-Explained
class Solution: def thirdMax(self, nums: List[int]) -> int: # Initialize the priority queue heap = [] for num in nums: heapq.heappush(heap, -num) seen, maxes = set(), 1 # Remember the first max of the list finalMax = -heapq.heappop(heap) heapq.heappush(heap, -finalMax) # Keep removing from the priority queue until we spot the third distinct # max element or until the priority empties. while len(heap) != 0: ele = heapq.heappop(heap) if ele in seen: continue if maxes == 3: return -ele seen.add(ele) maxes += 1 return finalMax ```
third-maximum-number
Python Using Heapq - Explained
TheGreatMuffinMan
0
145
third maximum number
414
0.326
Easy
7,332
https://leetcode.com/problems/third-maximum-number/discuss/1382246/Python3-Simple-Solution-Faster-Than-100-with-Comments
class Solution: def thirdMax(self, nums: List[int]) -> int: # Remove redundant element nums = set(nums) # Convert it back to list to use sort nums = list(nums) # Sort the list nums.sort() # Return the third one from the last if not exist then return the last one return nums[-3] if len(nums) >= 3 else nums[-1]
third-maximum-number
Python3 Simple Solution Faster Than 100% with Comments
Hejita
0
155
third maximum number
414
0.326
Easy
7,333
https://leetcode.com/problems/add-strings/discuss/1591891/Python-Straightforward-Solution-or-Easy-to-Understand
class Solution: def addStrings(self, num1: str, num2: str) -> str: num1 = list(num1) num2 = list(num2) car = 0 res = "" while num1 or num2 or car: if num1: car += int(num1.pop()) if num2: car += int(num2.pop()) res += str((car % 10)) car //= 10 return res[::-1]
add-strings
Python Straightforward Solution | Easy to Understand
leet_satyam
10
605
add strings
415
0.526
Easy
7,334
https://leetcode.com/problems/add-strings/discuss/1330543/Simple-Python-Solution-for-%22Add-Strings%22
class Solution: def addStrings(self, num1: str, num2: str) -> str: def func(n): value = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9} result = 0 for digit in n: result = 10 * result + value[digit] return result ans = func(num1) + func(num2) return str(ans)
add-strings
Simple Python Solution for "Add Strings"
sakshikhandare2527
4
364
add strings
415
0.526
Easy
7,335
https://leetcode.com/problems/add-strings/discuss/1268970/Python-solution
class Solution: def addStrings(self, num1: str, num2: str) -> str: s = {str(i):i for i in range(10)} def helper(num: str, ret: int = 0) -> int: for ch in num: ret = ret * 10 + s[ch] return ret return str(helper(num1) + helper(num2))
add-strings
Python solution
5tigerjelly
4
732
add strings
415
0.526
Easy
7,336
https://leetcode.com/problems/add-strings/discuss/2118139/Python-without-int()-using-string-as-keys-in-dictionary
class Solution: def addStrings(self, num1: str, num2: str) -> str: NUMBERS = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9} def make_integer(num): num_integer = 0 num_length = len(num) - 1 for digit in num: num_integer += NUMBERS[digit] * 10 ** num_length num_length -= 1 return num_integer return str(make_integer(num1) + make_integer(num2))
add-strings
Python without int(), using string as keys in dictionary
cosinushh
3
289
add strings
415
0.526
Easy
7,337
https://leetcode.com/problems/add-strings/discuss/1760735/Easy-Python-without-int()
class Solution: def addStrings(self, num1: str, num2: str) -> str: def numToStr(s): n=0 for i in s: n=n*10+ord(i)-ord('0') return n n,m=numToStr(num1),numToStr(num2) return str(m+n)
add-strings
Easy Python without int()
adityabaner
3
372
add strings
415
0.526
Easy
7,338
https://leetcode.com/problems/add-strings/discuss/1448713/Python3Python-Simple-solution-w-comments
class Solution: def addStrings(self, a: str, b: str) -> str: n = len(a) m = len(b) val = "" carry = 0 # Loop till all elements are exhausted while n or m: # Assign carry to num num = carry # for string "a", add to num if n: num = num + int(a[n-1]) n = n - 1 # for string "b", add to num if m: num = num + int(b[m-1]) m = m - 1 # compute next carry carry = num//10 # Add unit digit of num to value string val = str(num%10) + val # If carry, add carry to value string if carry: val = str(carry)+val return val # return value string
add-strings
[Python3/Python] Simple solution w/ comments
ssshukla26
2
283
add strings
415
0.526
Easy
7,339
https://leetcode.com/problems/add-strings/discuss/2408670/Long-code-but-easy-to-understand-and-good-for-interview
class Solution: def addStrings(self, num1: str, num2: str) -> str: if len(num1)<len(num2): return self.addStrings(num2,num1) n=len(num1)-1 m=len(num2)-1 num1=[i for i in num1] num2=[i for i in num2] flag=0 while m>=0: temp= int(num1[n])+int(num2[m])+flag if temp>9: flag=1 else: flag=0 num1[n]=str(temp%10) n-=1 m-=1 # print(num1,flag) while flag and n>=0: temp=int(num1[n])+flag if temp>9: flag=1 else: flag=0 # print(n,temp%10) num1[n]=str(temp%10) n-=1 if flag and n==-1: return '1'+''.join(num1) return ''.join(num1)
add-strings
Long code but easy to understand and good for interview
prateekgoel7248
1
87
add strings
415
0.526
Easy
7,340
https://leetcode.com/problems/add-strings/discuss/1458265/Simple-Python-O(n)-carry-digit-solution
class Solution: def addStrings(self, num1: str, num2: str) -> str: p1, p2 = len(num1)-1, len(num2)-1 ret = [] carry = 0 while p1 >= 0 or p2 >= 0 or carry: d1 = int(num1[p1]) if p1 >= 0 else 0 d2 = int(num2[p2]) if p2 >= 0 else 0 sum = d1+d2+carry carry, digit = sum//10, sum%10 ret.append(str(digit)) p1 -= 1 p2 -= 1 return "".join(ret[::-1])
add-strings
Simple Python O(n) carry digit solution
Charlesl0129
1
303
add strings
415
0.526
Easy
7,341
https://leetcode.com/problems/add-strings/discuss/1403833/python3-Simply-Addition-in-primary-School
class Solution: def addStrings(self, num1: str, num2: str) -> str: num1 = num1[::-1] num2 = num2[::-1] carry = i = 0 num3 = '' l1 = len(num1) l2 = len(num2) l3 = max(l1,l2) while i < l3 or carry: d1 = int(num1[i]) if i < l1 else 0 d2 = int(num2[i]) if i < l2 else 0 d3 = (d1 + d2 + carry) % 10 #่ฆๅŠ ๅˆฐnum3ไธญ็š„ๆ•ฐ carry = (d1 + d2 + carry) // 10 #ๆ˜ฏๅฆไธ‹ไธ€ไฝ้œ€่ฆ่ฟ›1 num3 += str(d3) i += 1 return num3[::-1]
add-strings
python3 Simply Addition in primary School
yjin232
1
202
add strings
415
0.526
Easy
7,342
https://leetcode.com/problems/add-strings/discuss/1227070/Python3-simple-solution-without-using-int()-function
class Solution: def addStrings(self, num1: str, num2: str) -> str: n, m = len(num1), len(num2) if n < m: num1 = (m-n)*'0' + num1 if m < n: num2 = (n-m)*'0' + num2 res = 0 n = len(num1) c = 0 for i,j in zip(num1, num2): res += (ord(i)-48 + ord(j)-48)* 10**(n-c-1) c += 1 return str(res)
add-strings
Python3 simple solution without using int() function
EklavyaJoshi
1
208
add strings
415
0.526
Easy
7,343
https://leetcode.com/problems/add-strings/discuss/762749/Python-Recursive-solution-with-HashMap-(ONLY-STRINGS)
class Solution: addMap = {str(n):{str(i) : str(n+i) for i in range(10)} for n in range(10)} def addStrings(self, num1: str, num2: str) -> str: if num1 == '0' or not num1: return num2 if num2 == '0' or not num2: return num1 carry,temp = '0', '' minN = min(len(num1), len(num2)) for i in range(1, 1+minN): a,b = num1[-i], num2[-i] add = self.addMap.get(a).get(b) total = self.addStrings(add,carry) temp = total[-1] + temp carry = total[:-1] if len(total) >1 else '0' remain = num1[:-minN] + num2[:-minN] temp = ( remain if carry =='0' else self.addStrings(remain,carry)) +temp return temp
add-strings
Python Recursive solution with HashMap (ONLY STRINGS)
amrmahmoud96
1
312
add strings
415
0.526
Easy
7,344
https://leetcode.com/problems/add-strings/discuss/598345/Faster-than-75-solution-Python-Clear
class Solution: def addStrings(self, num1: str, num2: str) -> str: res1,res2= 0,0 for i in num1 : res1 = res1*10 + (ord(i) - ord('0')) for i in num2 : res2 = res2*10 + (ord(i) - ord('0')) return str(res1+res2)
add-strings
Faster than 75 % solution Python Clear
vichu006
1
268
add strings
415
0.526
Easy
7,345
https://leetcode.com/problems/add-strings/discuss/581756/Python3-solution-using-basic-addition-and-backwards-iteration
class Solution: def addStrings(self, num1: str, num2: str) -> str: result = "" carry = 0 num1_pointer = len(num1) - 1 num2_pointer = len(num2) - 1 while num1_pointer >= 0 or num2_pointer >= 0: digit1 = 0 digit2 = 0 if num1_pointer >= 0: digit1 = int(num1[num1_pointer]) num1_pointer -= 1 if num2_pointer >= 0: digit2 = int(num2[num2_pointer]) num2_pointer -= 1 current_digit = digit1 + digit2 + carry carry = current_digit // 10 current_digit = current_digit % 10 result = self.add_digits(current_digit, result) if carry != 0: result = self.add_digits(carry, result) return result def add_digits(self, a, b): b = str(a) + b return b
add-strings
Python3 solution using basic addition and backwards iteration
scandinavian_swimmer
1
222
add strings
415
0.526
Easy
7,346
https://leetcode.com/problems/add-strings/discuss/558204/Python3-calculate-digits-one-by-one
class Solution: def addStrings(self, num1: str, num2: str) -> str: """Add two numbers represented as strings""" ans = [None]*(m:=max(len(num1), len(num2))) carry = 0 for i in range(m): if i < len(num1): carry += ord(num1[~i]) - 48 if i < len(num2): carry += ord(num2[~i]) - 48 carry, ans[i] = divmod(carry, 10) if carry: ans.append(carry) return "".join(map(str, reversed(ans)))
add-strings
[Python3] calculate digits one-by-one
ye15
1
172
add strings
415
0.526
Easy
7,347
https://leetcode.com/problems/add-strings/discuss/558204/Python3-calculate-digits-one-by-one
class Solution: def addStrings(self, num1: str, num2: str) -> str: ans = [] i = carry = 0 while i < len(num1) or i < len(num2) or carry: if i < len(num1): carry += ord(num1[~i])-48 if i < len(num2): carry += ord(num2[~i])-48 carry, v = divmod(carry, 10) ans.append(v) i += 1 return "".join(map(str, reversed(ans)))
add-strings
[Python3] calculate digits one-by-one
ye15
1
172
add strings
415
0.526
Easy
7,348
https://leetcode.com/problems/add-strings/discuss/530632/Really-easy-to-understand-python-solution-using-zfill
class Solution: def addStrings(self, num1: str, num2: str) -> str: max_len = max(len(num1), len(num2)) #so that you always get 0 even if one str is longer than other num1 = num1.zfill(max_len) num2 = num2.zfill(max_len) result = [] carry = 0 for i in range(max_len - 1, -1, -1): sum_ans = sum(map(int, [num1[i], num2[i], carry])) result.append(sum_ans % 10) carry = sum_ans//10 if carry: result.append(carry) return ''.join(map(str, result))[::-1]
add-strings
Really easy to understand python solution using zfill
user2320I
1
251
add strings
415
0.526
Easy
7,349
https://leetcode.com/problems/add-strings/discuss/2843159/Pythonic-solution-with-respect-to-all-the-constraints-provided
class Solution: def addStrings(self, num1: str, num2: str) -> str: dic = {"0":0, "1":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9} car = 0 if len(num1)>len(num2): num2 = "0"*(len(num1)-len(num2)) + num2 elif len(num1)<len(num2): num1 = "0"*(len(num2)-len(num1)) + num1 num1, num2 = num2, num1 res = "" for i in range(len(num1)-1, -1, -1): t = dic[num1[i]] + dic[num2[i]] + car if t>9: car = int(t/10) t = t%10 else: car = 0 res += str(t) if car>0: res += str(car) return res[::-1]
add-strings
Pythonic solution with respect to all the constraints provided
AkashwithTech
0
4
add strings
415
0.526
Easy
7,350
https://leetcode.com/problems/add-strings/discuss/2842299/python-one-line-(Beats-98-Memory-13.8-MB-Beats-99.31)
class Solution: def addStrings(self, num1: str, num2: str) -> str: return str(int(num1) + int(num2));
add-strings
python one line (Beats 98% Memory 13.8 MB Beats 99.31%)
seifsoliman
0
5
add strings
415
0.526
Easy
7,351
https://leetcode.com/problems/add-strings/discuss/2833683/Python-1-liner-no-str()-and-int()
class Solution: def addStrings(self, num1: str, num2: str) -> str: return '%s'%sum([sum([elem*(10**ind) for ind, elem in enumerate([ord(i)-48 for i in l][::-1])]) for l in (num1, num2)])
add-strings
Python 1-liner, no str() and int()
nguyentuduck
0
3
add strings
415
0.526
Easy
7,352
https://leetcode.com/problems/add-strings/discuss/2826985/Iterate-from-the-end-of-the-two-strings-Python3
class Solution: def addStrings(self, num1: str, num2: str) -> str: out = "" # Set the two strings to same size length = max(len(num1), len(num2)) num1 = num1.zfill(length) num2 = num2.zfill(length) # Start from the end of the string index = -1 # Indicate if we have additional number based on the previous addition rest = 0 while index > -length -1: # Get the value based on the ASCII value value = ord(num1[index]) + ord(num2[index]) - 2 * ord('0') value += rest rest = value // 10 out = str(value % 10) + out index -= 1 # Don't forget to add the last remaining rest if it exists if rest > 0: out = str(rest) + out return out
add-strings
Iterate from the end of the two strings [Python3]
rere-rere
0
3
add strings
415
0.526
Easy
7,353
https://leetcode.com/problems/add-strings/discuss/2812644/LeetCode-String-Problem
class Solution: def addStrings(self, num1:str, num2:str) -> str: #ord #1 - 49 #2 - 50 #return str(int(num1) + int(num2)) N, M = len(num1), len(num2) a,b = N-1,M-1 carry = 0 output = '' while a>=0 or b>=0: i,j=0,0 if a>=0: i = ord(num1[a]) - 48 a-=1 if b>=0: j = ord(num2[b]) - 48 b-=1 temp = i + j + carry if temp > 9: carry = 1 else: carry=0 output = str(temp)[-1] + output if carry: output = "1" + output return output
add-strings
LeetCode String Problem
ashwinpertigu
0
2
add strings
415
0.526
Easy
7,354
https://leetcode.com/problems/add-strings/discuss/2800367/Python-oror-simplest-solution-ever.
class Solution: def addStrings(self, num1: str, num2: str) -> str: return str(self.calculateSum(num1) + self.calculateSum(num2)) def calculateSum(self, num: str) -> int: ans = 0 factor = 1 end = len(num) - 1 while end >= 0: ans += int(num[end]) * factor factor *= 10 end -= 1 return ans
add-strings
Python || simplest solution ever.
ahmedsamara
0
5
add strings
415
0.526
Easy
7,355
https://leetcode.com/problems/add-strings/discuss/2781364/python-without-int()-function
class Solution: def addStrings(self, num1: str, num2: str) -> str: ans = "" size1, size2 = len(num1) - 1, len(num2) - 1 vals = 0 while size1>=0 or size2>=0 or vals: if size1>=0: vals += ord(num1[size1])-48 size1 -= 1 if size2>=0: vals += ord(num2[size2])-48 size2 -= 1 ans = str(vals%10) + ans vals = math.floor(vals//10) return ans
add-strings
python without int() function
game50914
0
3
add strings
415
0.526
Easy
7,356
https://leetcode.com/problems/add-strings/discuss/2774968/Clean-Readable-Python-Solution-(Beats-8080)
class Solution: def addStrings(self, num1: str, num2: str) -> str: offset = 0 total = 0 result = '' len_1 = len(num1) - 1 len_2 = len(num2) - 1 while offset <= len_1 or offset <= len_2 or total: if offset <= len_1: total += int(num1[len_1 - offset]) if offset <= len_2: total += int(num2[len_2 - offset]) result = str(total % 10) + result total = 1 if total > 9 else 0 offset += 1 return result
add-strings
Clean Readable Python Solution (Beats 80%/80%)
trietostopme
0
4
add strings
415
0.526
Easy
7,357
https://leetcode.com/problems/add-strings/discuss/2742862/1-line-very-fast-code-python3
class Solution: def addStrings(self, num1: str, num2: str) -> str: return str(int(num1)+ int(num2))
add-strings
1 line very fast code python3
genaral-kg
0
7
add strings
415
0.526
Easy
7,358
https://leetcode.com/problems/add-strings/discuss/2738632/Simple-Python-Solution
class Solution: def addStrings(self, num1: str, num2: str) -> str: b = int(num1) c = int(num2) d = b + c e = str(d) return e
add-strings
Simple Python Solution
dnvavinash
0
3
add strings
415
0.526
Easy
7,359
https://leetcode.com/problems/add-strings/discuss/2737326/Python-23-Lines
class Solution: def addStrings(self, num1: str, num2: str) -> str: num_1 = int(num1) num_2 = int(num2) return str(num_1 + num_2)
add-strings
Python 2/3 Lines
lucasschnee
0
9
add strings
415
0.526
Easy
7,360
https://leetcode.com/problems/add-strings/discuss/2733599/Simple-3-lines-of-code-in-Python
class Solution: def addStrings(self, num1: str, num2: str) -> str: n1 = int(''.join(num1)) #converting num1 to integer n2 = int(''.join(num2)) # converting num2 to integer summ = n1 + n2 # adding num1 and num2 after converting them to integer return f"{summ}" #returning the sum using string formating by f-string
add-strings
๐Ÿคฉ Simple 3 lines of code in Python ๐Ÿ˜Ž
mohitdubey1024
0
5
add strings
415
0.526
Easy
7,361
https://leetcode.com/problems/add-strings/discuss/2728954/Beats-86.83-Runtime-Beats-59.91-Memory
class Solution: def addStrings(self, num1: str, num2: str) -> str: n1 = len(num1) - 1 n2 = len(num2) - 1 carry = 0 ans = '' while n1 >= 0 or n2 >= 0 or carry > 0: n1_digit = int(num1[n1]) if n1 >= 0 else 0 n2_digit = int(num2[n2]) if n2 >= 0 else 0 number = n1_digit + n2_digit + carry if number >= 10: ans = str(number % 10) + ans carry = 1 else: ans = str(number) + ans carry = 0 n1 -= 1 n2 -= 1 return ans
add-strings
Beats 86.83% Runtime; Beats 59.91% Memory
yaha12
0
7
add strings
415
0.526
Easy
7,362
https://leetcode.com/problems/add-strings/discuss/2711947/Python-Easy-to-understand-solution-with-String-manipulation-with-Explaination
class Solution: def addStrings(self, num1: str, num2: str) -> str: if len(num1)<len(num2): num1, num2 = num2, num1 l1 = list(num1) l2 = list(num2) if len(l1)!=len(l2): l2 = ['0']*(len(l1)-len(l2))+l2 output = [] carry = 0 for i in range(len(num1)-1,-1,-1): val = carry + int(l1[i]) + int(l2[i]) output.append(str(val%10)) carry = int(val//10) if carry!=0: output.append(str(carry)) return ''.join(str(e) for e in output[::-1])
add-strings
Python Easy to understand solution with String manipulation with Explaination
abrarjahin
0
11
add strings
415
0.526
Easy
7,363
https://leetcode.com/problems/add-strings/discuss/2710016/single-line-code
class Solution: def addStrings(self, num1: str, num2: str) -> str: return str(int(num1)+int(num2))
add-strings
single line code
sindhu_300
0
8
add strings
415
0.526
Easy
7,364
https://leetcode.com/problems/add-strings/discuss/2682339/Python-Simple-Sol.-Faster-then-80
class Solution: def addStrings(self, num1: str, num2: str) -> str: def helper(str): i=0 for s in str: i = (i*10) + ord(s) - ord('0') return i return str(helper(num1) + helper(num2))
add-strings
Python Simple Sol. Faster then 80%
pranjalmishra334
0
32
add strings
415
0.526
Easy
7,365
https://leetcode.com/problems/add-strings/discuss/2682190/Python-Solution
class Solution: def addStrings(self, num1: str, num2: str) -> str: n = int(num1) + int(num2) return str(n)
add-strings
Python Solution
Sheeza
0
5
add strings
415
0.526
Easy
7,366
https://leetcode.com/problems/add-strings/discuss/2655862/O(n.m)-creating-the-set-of-sums
class Solution: def canPartition(self, nums: List[int]) -> bool: total=sum(nums) if total%2==1: return False targetSum=total/2 subset_sum_set=set([0]) for n in nums: if targetSum-n in subset_sum_set: return True templist=[n+prev for prev in subset_sum_set] subset_sum_set=subset_sum_set.union(set(templist)) return False
add-strings
O(n.m) - creating the set of sums
ConfusedMoe
0
1
add strings
415
0.526
Easy
7,367
https://leetcode.com/problems/add-strings/discuss/2651658/Python-solution-without-using-ord
class Solution: def addStrings(self, num1: str, num2: str) -> str: # To adhere to the problem's constraints, we should probably hardcode this! tables_of_addition = {str(x): {str(i): str(x+i) for i in range(10)} for x in range(0, 10)} # Corner case for i in range(10, 20): tables_of_addition["1"][str(i)] = str(1+i) # Ensuring that the 2 numbers are the same size. num1 = num1.zfill(max(len(num1), len(num2))) num2 = num2.zfill(max(len(num1), len(num2))) p = len(num1)-1 carry, results = "0", "" while p > -1: curr_res = tables_of_addition[num1[p]][num2[p]] # Carry is a bit tricky since current result can be in the range 0, 18 (inclusive) curr_res_with_carry = tables_of_addition[str(carry)][curr_res] if str(carry) == "1" else curr_res carry = "0" # We know that the maximum addition (between 9 &amp; 9) is 2 digits and will equal 18 if len(curr_res_with_carry) > 1: carry = curr_res_with_carry[0] results = curr_res_with_carry[1]+results else: results = curr_res_with_carry[0]+results p-=1 return results if carry == "0" else carry + results
add-strings
Python solution without using ord
amegahed
0
34
add strings
415
0.526
Easy
7,368
https://leetcode.com/problems/add-strings/discuss/2516445/Python-Easy-To-Understand-Solution-for-Beginners
class Solution: def addStrings(self, num1: str, num2: str) -> str: def str2Num(num): numDict = { '0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9 } op = 0 for n in num: if n in numDict: op = op*10 + numDict[n] return op return str(str2Num(num1) + str2Num(num2))
add-strings
Python Easy To Understand Solution for Beginners
Ron99
0
108
add strings
415
0.526
Easy
7,369
https://leetcode.com/problems/add-strings/discuss/2479590/Python-or-easy-solution
class Solution: def addStrings(self, num1: str, num2: str) -> str: max_len = max(len(num1), len(num2)) # find maxumum length to feel the smallest with leading zero's num1 = num1.zfill(max_len) num2 = num2.zfill(max_len) res = '' leftover = 0 for n1, n2 in zip(num1[::-1], num2[::-1]): temp_sum = int(n1) + int(n2) + leftover res += str(temp_sum % 10) leftover = temp_sum//10 if leftover: res += '1' return res[::-1]
add-strings
Python | easy solution
Yauhenish
0
76
add strings
415
0.526
Easy
7,370
https://leetcode.com/problems/add-strings/discuss/2461638/Is-using-hashmap-acceptable-during-interview
class Solution: def addStrings(self, num1: str, num2: str) -> str: hashmap = {'1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '0':0} if len(num1)<len(num2): temp = num1 num1 = num2 num2 = temp #Always make num1 longer, for easier computation j = 0 result = 0 for i in range(len(num2)): # 0 --> len(num2)-1 # 1 --> len(num2)-1-1 # ... # i --> len(num2)-1-i s = hashmap[num2[len(num2)-1-i]] + hashmap[num1[len(num1)-1-i]] #Notice we should add from the last digit back from the first digit result += s*(10**i) j+=1 if j!=len(num1): for j in range(j, len(num1)): s = hashmap[num1[len(num1)-1-j]] result += s*(10**j) return str(result)
add-strings
Is using hashmap acceptable during interview?
Arana
0
63
add strings
415
0.526
Easy
7,371
https://leetcode.com/problems/add-strings/discuss/2335118/Python-93.12-faster-or-Simplest-solution-with-explanation-or-Beg-to-Adv-or-Math
class Solution: def addStrings(self, num1: str, num2: str) -> str: s1,s2=int(num1), int(num2) # as the provided numbers are string we cant add them, so first we are making them as int to perform addition. total = s1+s2 # adding 2 number. We cant add 2 numbers if their type is str using +, as it will result in concatenation. return str(total) # converting back the integer adding to a string result as per the question requirement.
add-strings
Python 93.12% faster | Simplest solution with explanation | Beg to Adv | Math
rlakshay14
0
129
add strings
415
0.526
Easy
7,372
https://leetcode.com/problems/add-strings/discuss/2335118/Python-93.12-faster-or-Simplest-solution-with-explanation-or-Beg-to-Adv-or-Math
class Solution: def addStrings(self, num1: str, num2: str) -> str: return str(int(num1)+int(num2)) # doing the same as in the above solution just in one line.
add-strings
Python 93.12% faster | Simplest solution with explanation | Beg to Adv | Math
rlakshay14
0
129
add strings
415
0.526
Easy
7,373
https://leetcode.com/problems/add-strings/discuss/1760414/Python-oror-Memory-Less-than-99.91-oror-Dictionaries
class Solution: def change(converter,string): num = 0 for i in string: num = num*10 + converter[i] return num def addStrings(self, num1: str, num2: str) -> str: converter = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9} return str(Solution.change(converter,num1) + Solution.change(converter,num2))
add-strings
Python || Memory Less than 99.91% || Dictionaries
Akhilesh_Pothuri
0
96
add strings
415
0.526
Easy
7,374
https://leetcode.com/problems/add-strings/discuss/1703409/Python3-easy-understand-solution
class Solution: def addStrings(self, num1: str, num2: str) -> str: s1, s2 = 0, 0 for i in num1: s1 = s1*10 + int(i) for i in num2: s2 = s2*10 + int(i) res = str(s1 + s2) return res
add-strings
Python3 easy understand solution
TikaCrota
0
251
add strings
415
0.526
Easy
7,375
https://leetcode.com/problems/add-strings/discuss/1638744/Separate-carry-and-addition-maybe-easier-to-implement-and-understand
class Solution: def addStrings(self, num1: str, num2: str) -> str: def dig(c): return ord(c) - ord('0') n1 = len(num1) n2 = len(num2) res = [0]*(max(n1, n2) + 1) p1 = n1 - 1 p2 = n2 - 1 p = len(res) - 1 while p1 >= 0 or p2 >= 0: if p1 >= 0: res[p] += dig(num1[p1]) p1 -= 1 if p2 >= 0: res[p] += dig(num2[p2]) p2 -= 1 p -= 1 # carry the results carry = 0 p = len(res) - 1 while p >= 0: res[p] += carry carry = res[p] // 10 res[p] = res[p] % 10 p -= 1 result = '' p = 0 while p < len(res) and res[p] == 0: p += 1 if p == len(res): return '0' while p < len(res): result += str(res[p]) p += 1 return result
add-strings
Separate carry and addition - maybe easier to implement and understand
hl2425
0
108
add strings
415
0.526
Easy
7,376
https://leetcode.com/problems/add-strings/discuss/1538987/Python
class Solution: def addStrings(self, num1: str, num2: str) -> str: res = "" i = len(num1) - 1 j = len(num2) - 1 flag = 0 while i >=0 or j >= 0: a = int(num1[i]) if i >=0 else 0 i = i - 1 b = int(num2[j]) if j >=0 else 0 j = j - 1 sum = a + b + flag res = str(sum % 10 ) + res flag = sum / 10 return res if flag == 0 else (str(flag)+ res)
add-strings
Python
siyu14
0
187
add strings
415
0.526
Easy
7,377
https://leetcode.com/problems/add-strings/discuss/1538987/Python
class Solution: def addStrings(self, num1: str, num2: str) -> str: sum1 = 0 sum2 = 0 length1 = len(num1) - 1 length2 = len(num2) - 1 for i in range(len(num1)): digit = ord(num1[i]) - ord('0') sum1 += digit * pow(10, length1) length1 -= 1 for i in range(len(num2)): digit = ord(num2[i]) - ord('0') sum2 += digit * pow(10, length2) length2 -= 1 return str(sum1 + sum2)
add-strings
Python
siyu14
0
187
add strings
415
0.526
Easy
7,378
https://leetcode.com/problems/add-strings/discuss/1452830/Python3-Simple-clean-and-easy-solution-O(N)
class Solution: def addStrings(self, num1: str, num2: str) -> str: i, j, carry = len(num1)-1, len(num2)-1, 0 res = [] while i >= 0 or j >= 0 or carry > 0: n1 = 0 n2 = 0 if i >= 0: n1 = int(num1[i]) i -= 1 if j >= 0: n2 = int(num2[j]) j -= 1 summ = n1 + n2 + carry carry = 1 if summ >= 10 else 0 res.insert(0, str(summ % 10)) return ''.join(res)
add-strings
[Python3] Simple clean and easy solution O(N)
sshaplygin
0
322
add strings
415
0.526
Easy
7,379
https://leetcode.com/problems/add-strings/discuss/1324890/python-solution
class Solution: def addStrings(self, num1: str, num2: str) -> str: #๊ฐ ๋ถ€์กฑํ•œ ์ž๋ฆฟ์ˆ˜ ๋งŒํผ 0์œผ๋กœ ์ฑ„์šฐ๊ธฐ if len(num1) > len(num2): num2 = '0' * (len(num1) - len(num2)) + num2 if len(num2) > len(num1): num1 = '0' * (len(num2) - len(num1)) + num1 result = '' add = '0' for i, j in zip(num1[::-1], num2[::-1]): num = int(i) + int(j) + int(add) if num >= 10: add = '1' result = str(num % 10) + result else: result = str(num) + result add = '0' if add == '1': result = add + result return result
add-strings
python solution
koreahong2
0
98
add strings
415
0.526
Easy
7,380
https://leetcode.com/problems/add-strings/discuss/1263469/Python-dollarolution
class Solution: def addStrings(self, num1: str, num2: str) -> str: c, u = 0, '' if len(num1) > len(num2): temp = num1 num1 = num2 num2 = temp for i in range(1, len(num2)+1): if i <= len(num1): j = str(int(num1[-i])+int(num2[-i])+int(c)) if len(j) > 1: c = j[0] u = j[1] + u else: c = '0' u = j + u else: j = str(int(num2[-i])+int(c)) if len(j) > 1: c = j[0] u = j[1] + u else: c = '0' u = j + u if int(c) > 0: return (c + u) else: return u
add-strings
Python $olution
AakRay
0
226
add strings
415
0.526
Easy
7,381
https://leetcode.com/problems/add-strings/discuss/1260818/Python3-straight-forward-solution
class Solution: def addStrings(self, num1: str, num2: str) -> str: n1,n2=0,0 for i in num1: n1 = n1*10 + ord(i)-48 for i in num2: n2 = n2*10 + ord(i)-48 return str(n1+n2)
add-strings
Python3 straight forward solution
Sanyamx1x
0
133
add strings
415
0.526
Easy
7,382
https://leetcode.com/problems/add-strings/discuss/1223725/python-3-or-Self-defined-str-type
class Solution: def addStrings(self, num1: str, num2: str) -> str: if "0" in (num1, num2): return num2 if num1 == "0" else num1 class MyStr(str): dic = {str(v): v for v in range(10)} def __getitem__(self, index): try: return MyStr.dic[super().__getitem__(index)] except IndexError: return 0 num1, num2 = MyStr(num1), MyStr(num2) carry, s = 0, "" for idx in range(-1, -1-max(len(num1), len(num2)), -1): o_sum = num1[idx] + num2[idx] + carry carry = o_sum // 10 s = f'{o_sum % 10}{s}' return f'1{s}' if carry else s
add-strings
python 3 | Self-defined str type
mousun224
0
187
add strings
415
0.526
Easy
7,383
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1624391/Python-DP-and-DFS-Solutions-Easy-to-understand-with-Explanation
class Solution: def canPartition(self, nums: List[int]) -> bool: if sum(nums)%2: # or if sum(nums)&amp;1 return False # main logic here
partition-equal-subset-sum
[Python] DP & DFS Solutions - Easy-to-understand with Explanation
zayne-siew
38
5,800
partition equal subset sum
416
0.466
Medium
7,384
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1624391/Python-DP-and-DFS-Solutions-Easy-to-understand-with-Explanation
class Solution: def canPartition(self, nums: List[int]) -> bool: s = sum(nums) if s&amp;1: return False """ The dp array stores the total obtained sums we have come across so far. Notice that dp[0] = True; if we never select any element, the total sum is 0. """ dp = [True]+[False]*s # Now, loop through each element for num in nums: for curr in range(s, num-1, -1): # avoid going out-of-bounds """ Case 1: The current sum (curr) has been seen before. Then, if we don't select the current element, the sum will not change. So, this total sum will still exist, and its dp value remains True. Case 2: The current sum (curr) has not been seen before, but it can be obtained by selecting the current element. This means that dp[curr-num] = True, and thus dp[curr] now becomes True. Case 3: The current sum (curr) has not been seen before, and it cannot be obtained by selecting the current element. So, this total sum will still not exist, and its dp value remains False. """ dp[curr] = dp[curr] or dp[curr-num] # Finally, we want to obtain the target sum return dp[s//2] # or dp[s>>1]
partition-equal-subset-sum
[Python] DP & DFS Solutions - Easy-to-understand with Explanation
zayne-siew
38
5,800
partition equal subset sum
416
0.466
Medium
7,385
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1624391/Python-DP-and-DFS-Solutions-Easy-to-understand-with-Explanation
class Solution: def canPartition(self, nums: List[int]) -> bool: s = sum(nums) if s&amp;1: return False dp = [True]+[False]*(s>>1) for num in nums: for curr in range(s>>1, num-1, -1): dp[curr] = dp[curr] or dp[curr-num] return dp[-1]
partition-equal-subset-sum
[Python] DP & DFS Solutions - Easy-to-understand with Explanation
zayne-siew
38
5,800
partition equal subset sum
416
0.466
Medium
7,386
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1624391/Python-DP-and-DFS-Solutions-Easy-to-understand-with-Explanation
class Solution: def canPartition(self, nums: List[int]) -> bool: dp, s = set([0]), sum(nums) if s&amp;1: return False for num in nums: for curr in range(s>>1, num-1, -1): if curr not in dp and curr-num in dp: if curr == s>>1: return True dp.add(curr) return False
partition-equal-subset-sum
[Python] DP & DFS Solutions - Easy-to-understand with Explanation
zayne-siew
38
5,800
partition equal subset sum
416
0.466
Medium
7,387
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1624391/Python-DP-and-DFS-Solutions-Easy-to-understand-with-Explanation
class Solution: def canPartition(self, nums: List[int]) -> bool: dp, s = set([0]), sum(nums) if s&amp;1: return False for num in nums: dp.update([v+num for v in dp if v+num <= s>>1]) return s>>1 in dp
partition-equal-subset-sum
[Python] DP & DFS Solutions - Easy-to-understand with Explanation
zayne-siew
38
5,800
partition equal subset sum
416
0.466
Medium
7,388
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1624391/Python-DP-and-DFS-Solutions-Easy-to-understand-with-Explanation
class Solution: def canPartition(self, nums: List[int]) -> bool: l, s = len(nums), sum(nums) @cache # this is important to avoid unnecessary recursion def dfs(curr: int, idx: int) -> bool: """ Select elements and check if nums can be partitioned. :param curr: The current sum of the elements in the subset. :param idx: The index of the current element in nums. :return: True if nums can be partitioned, False otherwise. """ if idx == l: # we have reached the end of the array return curr == s>>1 elif curr+nums[idx] == s>>1 or \ # target sum obtained (curr+nums[idx] < s>>1 and \ dfs(curr+nums[idx], idx+1)): # or, target sum will be reached by selecting this element return True return dfs(curr, idx+1) # else, don't select this element, continue return False if s&amp;1 else dfs(0, 0)
partition-equal-subset-sum
[Python] DP & DFS Solutions - Easy-to-understand with Explanation
zayne-siew
38
5,800
partition equal subset sum
416
0.466
Medium
7,389
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1624391/Python-DP-and-DFS-Solutions-Easy-to-understand-with-Explanation
class Solution: def canPartition(self, nums: List[int]) -> bool: l, s = len(nums), sum(nums) @cache def dfs(curr: int, idx: int) -> bool: return curr == s>>1 if idx == l else True if curr+nums[idx] == s>>1 or \ (curr+nums[idx] < s>>1 and dfs(curr+nums[idx], idx+1)) else dfs(curr, idx+1) return False if s&amp;1 else dfs(0, 0)
partition-equal-subset-sum
[Python] DP & DFS Solutions - Easy-to-understand with Explanation
zayne-siew
38
5,800
partition equal subset sum
416
0.466
Medium
7,390
https://leetcode.com/problems/partition-equal-subset-sum/discuss/872024/Python3-Brute-Force-greater-Top-Down-greater-Bottom-Up
class Solution: def canPartition(self, nums: List[int]) -> bool: if not nums: return True n = len(nums) if sum(nums) % 2 != 0: return False target = sum(nums)//2 answer = 0 def helper(total, i): nonlocal nums, answer if i == len(nums): return if total == 0: answer += 1 helper(total-nums[i], i+1) helper(total, i+1) helper(target, 0) return answer
partition-equal-subset-sum
Python3 Brute Force -> Top Down -> Bottom Up
nyc_coder
10
573
partition equal subset sum
416
0.466
Medium
7,391
https://leetcode.com/problems/partition-equal-subset-sum/discuss/872024/Python3-Brute-Force-greater-Top-Down-greater-Bottom-Up
class Solution: def canPartition(self, nums: List[int]) -> bool: if not nums: return True n = len(nums) if sum(nums) % 2 != 0: return False target = sum(nums)//2 memo = {} def helper(total, i): nonlocal nums, memo if (total, i) in memo: return memo[(total, i)] if i == len(nums): return False if total == 0: return True memo[(total, i)] = helper(total-nums[i], i+1) or helper(total, i+1) return memo[(total, i)] return helper(target, 0)
partition-equal-subset-sum
Python3 Brute Force -> Top Down -> Bottom Up
nyc_coder
10
573
partition equal subset sum
416
0.466
Medium
7,392
https://leetcode.com/problems/partition-equal-subset-sum/discuss/872024/Python3-Brute-Force-greater-Top-Down-greater-Bottom-Up
class Solution: def canPartition(self, nums: List[int]) -> bool: if not nums: return True n = len(nums) if sum(nums) % 2 != 0: return False target = sum(nums)//2 dp = [[False for _ in range(target+1)] for _ in range(n+1)] dp[0][0] = True for i in range(1, n+1): for j in range(target+1): if j < nums[i-1]: dp[i][j] = dp[i-1][j] else: dp[i][j] = dp[i-1][j] or dp[i-1][j-nums[i-1]] if j == target and dp[i][j]: return True return False
partition-equal-subset-sum
Python3 Brute Force -> Top Down -> Bottom Up
nyc_coder
10
573
partition equal subset sum
416
0.466
Medium
7,393
https://leetcode.com/problems/partition-equal-subset-sum/discuss/872024/Python3-Brute-Force-greater-Top-Down-greater-Bottom-Up
class Solution: def canPartition(self, nums: List[int]) -> bool: if not nums: return True n = len(nums) if sum(nums) % 2 != 0: return False target = sum(nums)//2 dp = [False for _ in range(target+1)] dp[0] = True for num in nums: for j in range(target, num-1, -1): dp[j] = dp[j] or dp[j-num] if dp[target]: return True return False
partition-equal-subset-sum
Python3 Brute Force -> Top Down -> Bottom Up
nyc_coder
10
573
partition equal subset sum
416
0.466
Medium
7,394
https://leetcode.com/problems/partition-equal-subset-sum/discuss/609458/Python3-2-lines-32ms-bitset-solution-Partition-Equal-Subset-Sum
class Solution: def canPartition(self, nums: List[int]) -> bool: target, r = divmod(sum(nums), 2) return r == 0 and (reduce(lambda x, y: x << y | x, [1] + nums) >> target) &amp; 1
partition-equal-subset-sum
Python3 2 lines, 32ms, bitset solution - Partition Equal Subset Sum
r0bertz
4
895
partition equal subset sum
416
0.466
Medium
7,395
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1632359/Recursion-%2B-memoization-in-Python-(174-ms)-beats-87.56-submission
class Solution: def canPartition(self, nums: List[int]) -> bool: def rec(idx, target): if (idx, target) in memo: return False if target == 0: return True elif target < 0 or idx == N: return False flag = rec(idx + 1, target - nums[idx]) or rec(idx + 1, target) if flag: return True memo.add((idx, target)) return False total_sum = sum(nums) #edge case if total_sum % 2 == 1: return False memo = set() N = len(nums) return rec(0, total_sum // 2)
partition-equal-subset-sum
Recursion + memoization in Python (174 ms), beats 87.56% submission
kryuki
2
375
partition equal subset sum
416
0.466
Medium
7,396
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1624420/Python3-MEMO-Explained
class Solution: def canPartition(self, nums: List[int]) -> bool: n = len(nums) s = sum(nums) if s%2: return False @cache def rec(i, one, two): if i == n: return not one and not two if one < 0 or two < 0: return False return rec(i + 1, one - nums[i], two) or rec(i + 1, one, two - nums[i]) return rec(1, s/2 - nums[0], s/2)
partition-equal-subset-sum
โœ”๏ธ[Python3] MEMO, Explained
artod
2
182
partition equal subset sum
416
0.466
Medium
7,397
https://leetcode.com/problems/partition-equal-subset-sum/discuss/1508882/Easiest-Approach-oror-Clean-and-Concise-Code-oror-For-Beginners
class Solution: def canPartition(self, nums: List[int]) -> bool: total = sum(nums) n = len(nums) if total%2: return False dp = dict() def backtrack(ind,local): if ind>=n: return False if total-local==local: return True if (ind,local) in dp: return dp[(ind,local)] dp[(ind,local)] = backtrack(ind+1,local+nums[ind]) or backtrack(ind+1,local) return dp[(ind,local)] return backtrack(0,0)
partition-equal-subset-sum
๐Ÿ“Œ๐Ÿ“Œ Easiest Approach || Clean & Concise Code || For Beginners ๐Ÿ
abhi9Rai
1
216
partition equal subset sum
416
0.466
Medium
7,398
https://leetcode.com/problems/partition-equal-subset-sum/discuss/838500/Python3-knapsack-solved-by-top-down-dp
class Solution: def canPartition(self, nums: List[int]) -> bool: if (ss := sum(nums)) &amp; 1: return False @lru_cache(None) def fn(i, v): """Return True if possible to find subarray of nums[i:] summing to v.""" if v <= 0: return v == 0 if i == len(nums): return False return fn(i+1, v) or fn(i+1, v - nums[i]) return fn(0, ss//2)
partition-equal-subset-sum
[Python3] knapsack solved by top-down dp
ye15
1
118
partition equal subset sum
416
0.466
Medium
7,399