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:
... | 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 ... | 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[... | 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
... | 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]
... | 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... | 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 neg... | 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 a... | 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
... | 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]
... | 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 = s... | 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
... | 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... | 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
... | 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, -... | 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... | 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
... | 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:
... | 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
... | 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:
... | 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
... | 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... | 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 +... | 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(l... | 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:
... | 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 += o... | 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)
... | 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 ... | 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(nu... | 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
# Ind... | 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:... | 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
... | 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
... | 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 ... | 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}" #retur... | 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
... | 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):
... | 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+... | 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_ad... | 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,
... | 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
... | 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... | 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 wil... | 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}
... | 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... | 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 - ... | 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
... | 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 >=... | 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-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])... | 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:
... | 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)&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&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.
... | 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&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&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:
... | 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&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 c... | 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, i... | 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
... | 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
... | 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
... | 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:
... | 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) & 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
fla... | 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 < ... | 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
... | 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)) & 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 == l... | partition-equal-subset-sum | [Python3] knapsack solved by top-down dp | ye15 | 1 | 118 | partition equal subset sum | 416 | 0.466 | Medium | 7,399 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.