LeetCode 066 Plus one

题目:

https://leetcode.com/problems/plus-one/
Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.

题目虽简单,但是要注意的是当最高位是9的时候, 需要重新new一个长一位的新数组来存储结果。

public class Solution {  
    public int[] plusOne(int[] digits) {  
        if (digits == null || digits.length == 0) return digits;  
        int carry = 1;  
        for (int i=digits.length - 1; i >= 0; i--) {  
            digits[i] = digits[i] + carry;  
            if (digits[i] < 10) {  
                return digits;  
            } else {  
                digits[i] = 0;  
                carry = 1;  
            }  
        }  
        int[] newDigits = new int[digits.length + 1];  
        newDigits[0] = 1;  
        System.arraycopy(digits, 0, newDigits, 1, digits.length);  
        return newDigits;  
    }  
}  

作者:ywheel
本文出处:http://blog.ywheel.com/post/2015/03/11/leetcode_66/
文章版权归本人所有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。