TreeviewCopyright © aleen42 all right reserved, powered by aleen42
66. 加一
https://leetcode-cn.com/problems/plus-one/
Java
class Solution {
public int[] plusOne(int[] digits) {
for(int i = digits.length - 1; i >= 0; i--) {
digits[i]++;
digits[i] = digits[i] % 10;
if(digits[i] != 0) return digits;
}
digits = new int[digits.length + 1];
digits[0] = 1;
return digits;
}
}
Python
'''
Author: Goog Tech
Date: 2020-08-25 11:46:58
LastEditTime: 2020-08-25 11:49:44
Description: https://leetcode-cn.com/problems/plus-one/
FilePath: \leetcode-googtech\#66. Plus One\Solution.py
WebSite: https://algorithm.show/
'''
class Solution(object):
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] = 0
else:
digits[i] = digits[i] + 1
return digits
if digits[0] == 0:
digits.insert(0, 1)
return digits