加一 (Plus One)

 

思路:

// @Title: 加一 (Plus One)
// @Author: qisiii
// @Date: 2021-08-16 19:51:43
// @Runtime: 0 ms
// @Memory: 36.9 MB
// @comment: 
// @flag: 
class Solution {
    public int[] plusOne(int[] digits) {
        int current=1;
        for (int i = digits.length-1; i >= 0; i--) {
            int digit = digits[i];
            digits[i]=(digit+current)%10;
            current=(digit+current)/10;
        }
        if (current==1){
            int[] result=new int[digits.length+1];
            result[0]=1;
            for (int i = 1; i < result.length; i++) {
                result[i]=digits[i-1];
            }
            return result;
        }
        return digits;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18