Plus One - Leetcode 66 - Python

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


0:00 - Read the problem
1:40 - Drawing Explanation
4:50 - Coding Explanation

leetcode 66

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

for i in range(len(digits) - 1, -1, -1):
if digits[i] == 9:
digits[i] = 0
else:
digits[i] += 1
return digits
return [1] + digits

danielsun
Автор

why dont we convert it [1, 2, 3] to normal interger like 123 and add one to it 124 and then we return it into array [1, 2, 4] !

saran
Автор

Here's my solution which uses recursion, lmk what y'all think:

class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
if not digits:
return [1]

if digits[-1] != 9:
digits[-1] += 1
return digits
return self.plusOne(digits[:-1]) + [0]

andymartinez
Автор

Using recursion, without reversing, & any loop.
class Solution:
n = 0
def plusOne(self, digits: List[int]) -> List[int]:
if digits[-1 - self.n] == 9:
digits[-1 - self.n] = 0
self.n += 1
if (len(digits) - self.n) <= 0:
digits.insert(0, 1)
else:
self.plusOne(digits)
else:
digits[-1 - self.n] += 1
return digits

DabhouMalataj
Автор

without reversing


class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
carry, i = 1, len(digits)-1
while carry:
if i >= 0:
if digits[i] == 9:
digits[i] = 0
else:
digits[i] += 1
carry = 0
elif i == -1:
digits = [carry]+digits
carry = 0
i -=1
return digits

geekydanish
Автор

Interesting solution. Here's what I did,
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
n = len(digits) - 1
carry = 1

while (carry and n != -1):
carry = (digits[n] + 1) // 10
digits[n] = (digits[n] + 1) % 10
n -= 1

if carry:
digits.insert(0, 1)
return digits

jeremyvidaurri
Автор

Love your work! Minor changes without reversing the digits .
i = len(digits)-1
while i >= 0:
if (digits[i] == 9):
digits[i] = 0
else:
digits[i] += 1
return digits
i -= 1
return [1] + digits

sivakrishnaviru
Автор

Using recursion, without reversing, & any loop.
class Solution:
n = 0
def plusOne(self, digits: List[int]) -> List[int]:
if digits[-1 - self.n] == 9:
digits[-1 - self.n] = 0
self.n += 1
if (len(digits) - self.n) <= 0:
digits.insert(0, 1)
else:
self.plusOne(digits)
else:
digits[-1 - self.n] += 1
return digits

DabhouMalataj
Автор

This was the first answer that popped into my mind.
return [int(c) for c in str(int("".join([str(c) for c in digits])) + 1 )]

mohammednasser
Автор

This is my solution, iterate from end to start, I think it is easier to understand, hope it can help.

class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
carry = 1

for i in range(len(digits) - 1, -1, -1):
digit = digits[i]
digits[i] = (digit + carry) % 10
carry = (digit + carry) // 10
if carry:
digits.insert(0, carry)
return digits

junjason
Автор

i don't know how to name my 'carry' variable so I'll call it one haha

qglbjxb
Автор

my solution
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
return self.addOne(digits, len(digits) - 1)

def addOne(self, digits, index):
if index == 0 and digits[index] == 9:
digits.insert(0, 1)
digits[1] = 0
return digits
if digits[index] == 9:
digits[index] = 0
self.addOne(digits, index-1)
else:
digits[index] += 1
return digits

raskovnic
Автор

For my language which is Kotlin it takes in an array and return an array. I wonder how you would do it with an array. I did it but the space complexity is like O(n).

KingFuYouTube
Автор

1 LINE python solution:






class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
return [int(digit) for digit in str(int("".join(map(lambda x: str(x), digits))) + 1)]

Famelhaut
Автор

I wasn't planning to look up the solution of this problem. But I like you and watched the video then I found out you break out the loop when it doesn't need to using the `one` variable, which I didn't. my loop always iterates until the end. Wow... The ability of figuring out you exactly have todo is really awesome. Thank you so much for uploading great videos!

licokr
Автор

Isn't reversing the array O(n) operation internally?

prestoX
Автор

Am I not supposed to do something like this:
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
temp = "".join(str(x) for x in digits)
temp = int(temp) + 1

return [int(x) for x in str(temp)]

hwang
Автор

nice explanation didnt think about reversing the list but now i have that in the toolbag

Ben_in_k
Автор

bro in javascript this is so simple, it also gets accepted in leetcode
var plusOne = function(digits) {
let inputinnumber = Number(digits.join(""))
let addition = inputinnumber+1
let result = String(addition).split('')
result = result.map(val=>{return parseInt(val)})
return result

};

danishmehmood
Автор

Seriously you had to explain why 9+1=10 can't be put in the last position? Really? Also one=0 is likely the worst line of code ever written. I know this is silly and you explained as being just lazy, but why though in a youtube video? You could just name it carry.

tachyon