66.加一

2018-05-15  本文已影响0人  _道友请留步_
class Solution {
    public int[] plusOne(int[] digits) {
        List<Integer> list = new ArrayList<>();
        int temp = 1;
        for (int i = digits.length-1; i >= 0; i--){
            list.add((digits[i]+temp)%10);
            temp = (digits[i]+temp)/10;
        }
        if(temp != 0){
            list.add(temp);
        }
        int[] result = new int[list.size()];
        for (int i = 0; i < list.size(); i++){
            result[list.size()-1-i] = list.get(i); 
        }
        return result;
    }
}
上一篇下一篇

猜你喜欢

热点阅读