首页 > 其他 > 详细

[LeetCode]Plus One

时间:2015-12-11 13:10:34      阅读:193      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!