Hello community! Happy New Year! Within two posts of practicing here, the universe decided to show some kindness, and I got an internship! I took the rest of the year off, so this year I'm hoping to be more productive and advance to the next level!
That being said, here's the solution i did for today's daily problem on leetcode.
Problem: 66. Plus One
Given an array representing a large integer, from left-right (msb-lsb), add 1 and return the new integer in a similar form.
My first approach:
def plusOne(self, digits: List[int]) -> List[int]:
dig = int("".join(str(x) for x in digits))
dig+= 1
dig = str(dig)
res = [int(i) for i in dig]
return res
A second approach would be to check if dig[i]==9, if it is, make it zero, and the dig[i-1]+=1. and we return the dig. no str->int conversions here.
Top comments (0)