[LeetCode][Python3] 66. Plus One
2018. 10. 15. 00:35 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/plus-one/description/
My Solution :
class Solution:
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
carry = 1
for i in range(len(digits)-1, -1, -1):
digit = (carry + digits[i]) % 10
carry = (carry + digits[i]) // 10
digits[i] = digit
if carry == 0:
return digits
return [1] + digits
Comment :
위 방법은 더하기 연산을 할 때의 일반적인 패턴을 활용한 것이고, 아래 방법은 이 문제에만 국한된 방법이다.
My Solution2 :
class Solution:
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
for i in range(len(digits)-1, -1, -1):
if digits[i] < 9:
digits[i] += 1
return digits
digits[i] = 0
return [1] + digits
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 122. Best Time to Buy and Sell Stock II (0) | 2018.11.03 |
---|---|
[LeetCode][Python3] 88. Merge Sorted Array (0) | 2018.11.03 |
[LeetCode][Python3] 118. Pascal's Triangle (0) | 2018.10.17 |
[LeetCode][Python3] 125. Valid Palindrome (0) | 2018.10.17 |
[LeetCode][Python3] 543. Diameter of Binary Tree (0) | 2018.10.09 |
[LeetCode][Python3] 617. Merge Two Binary Trees (0) | 2018.10.08 |
[LeetCode][Python3] 581. Shortest Unsorted Continuous Subarray (0) | 2018.10.08 |
[LeetCode][Python3] 572. Subtree of Another Tree (0) | 2018.10.07 |
최근에 달린 댓글 최근에 달린 댓글