Add Binary - Leetcode 67 - Python

preview_player
Показать описание


0:00 - Read the problem
0:25 - Drawing Explanation
2:20 - Coding Explanation

leetcode 67

#sorted #array #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

✏️ Have problem suggestions you would like me to solve? Feel free to comment them below :~)

NeetCode
Автор

an easy problem that can get boggled down in edge cases, really like some the tricks you used. great video as always!

yoshi
Автор

Love the way you write the code. Concise.
Love the loop of max of lengths.
Great explanation as usual

srinadhp
Автор

thanks, you're a lifesaver when it comes to leetcode

strawberriesandcream
Автор

You are a good teacher. Keep up the good work. Thank you!

akhileshb
Автор

Can't we just use int(a[i]) to cast it to an integer?

sunnilabeouf
Автор

Thank you for explaining what happens when you add "11" and "11". I was completely lost but not anymore thanks to you!

alexgolomb
Автор

This code is simply beautiful. Thanks.

beldam
Автор

What is the time complexity of the string concats in python(eg. char + res in the example above)

karthikkumaresan
Автор

Isn't the time complexity quadratic since the addition of the 'char' string to the 'res' string copies all the contents and creates a new string for every loop since strings in python are immutable?

rachnaramkumar
Автор

Hi at 5:10, couldnt you use int() instead of ord() to cast the char?

airysm
Автор

"Let's go back to elementary school and add these together. What happens when we add 1 and 😂

muzikjay
Автор

we could have appended 0* (a.size - b.size) to the shorter string, saving time in reversal of both the strings. Later reversing the final string

gagansuneja
Автор

I havent run the code, but did you get a character short each time from your reversing method.
a, b = reversed(a), reversed(b)

willd
Автор

def addBinary(self, a: str, b: str) -> str:
a = int(a, 2)
b = int(b, 2)
res = a+b
return bin(res)[2:]

vikasz
Автор

Great Explanation!
Might be cleaner to use the divmod() function instead of % and //.

Eg.
char, carry = divmod(total, 2)
res = str(char) + res

malh
Автор

we can use the same code for the question : leetcode 413(Add strings), just replace 2 by 10

BurhanAijaz
Автор

For converting it to integer, can we not do int(a[i]) and int(b[i]) instead of ord(a[i])-ord("0") and ord(b[i])-ord("0")?

sriramkarthikakella
Автор

Why didn't you use int function on the values a[i] and b[i]?

geraldakorli
Автор

The returned res should be reversed to get the original output.
return res[::-1]

sagargulati