-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path0407. Plus One
More file actions
28 lines (28 loc) · 779 Bytes
/
Copy path0407. Plus One
File metadata and controls
28 lines (28 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Solution {
/**
* @param digits: a number represented as an array of digits
* @return: the result
*/
public int[] plusOne(int[] digits) {
int length = digits.length;
if(digits[length-1] != 9){
digits[length-1]++;
return digits;
}
for(int i = digits.length-1; i >= 0; i--){ //units digits is 9
if(digits[i] == 9){
digits[i] = 0;
}
else{
digits[i]++;
return digits;
}
}
int[] result = new int[digits.length + 1]; //power of 10
result[0] = 1;
for(int i = 0; i < digits.length; i++){
result[i+1] = digits[i];
}
return result;
}
}