码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Plus One

时间:2015-12-11 13:11:31      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

public class Solution {
    public int[] plusOne(int[] digits) {
        int carry = 1;
        int length = digits.length;
        List<Integer> list = new ArrayList<Integer>();
        for (int i = length - 1; i >= 0; i--) {
            int digit = digits[i] + carry;
            if (digit > 9) {
                carry = 1;
                digit = digit - 10;
            } else {
                carry = 0;
            }
            list.add(digit);
        }
        if (carry != 0) {
            list.add(carry);
        }
        int[] result = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[list.size() - i - 1] = list.get(i);
        }
        return result;
    }
}

 

[LeetCode]Plus One

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5038595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!