`
hcx2013
  • 浏览: 90114 次
社区版块
存档分类
最新评论

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.

 

public class Solution {
    public int[] plusOne(int[] digits) {
        for (int i = digits.length-1; i >= 0; i--) {
        	digits[i] = digits[i]+1;
        	if (digits[i] == 10) {
        		digits[i] = 0;
        	} else {
        		return digits;
        	}
        }
        int[] newdigits = new int[digits.length+1];
        newdigits[0] = 1;
        for (int i = 1; i < newdigits.length; i++) {
        	newdigits[i] = digits[i-1];
        }
        return newdigits;
    }
}

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics