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.
很简单的一道题,没有啥要说的,直接accepted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int[] plusOne(int[] digits) { | |
if(digits == null || digits.length == 0) return digits; | |
for(int i=digits.length-1; i>=0; i--){ | |
if(digits[i]<9){ | |
digits[i]++; | |
return digits; | |
} | |
else{ | |
digits[i]=0; | |
} | |
} | |
int[] ret = new int[digits.length+1]; | |
ret[0]=1; | |
for(int i=0; i<digits.length; i++){ | |
ret[i+1] = digits[i]; | |
} | |
return ret; | |
} |
No comments:
Post a Comment