您的位置:首页 > 其它

Plus One [Easy]

2014-12-17 02:17 281 查看
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.

===============================Answer===============================================

public class Solution {
public int[] plusOne(int[] digits) {

if(digits==null||digits.length==0) return null;

for(int i=digits.length-1;i>=0;i--) {
if(digits[i]==9) digits[i]=0;
else {digits[i]=digits[i]+1; return digits;}

}

int[] returnList = new int[digits.length+1];
returnList[0]=1;
for(int i=1;i<returnList.length;i++) {
returnList[i]=0;
}
return returnList;

}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode Easy