Leetcode Algorithms-66.Plus One

问题地址

Plus One - LeetCode

问题描述

0.jpg

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Solution {

public int[] plusOne(int[] digits) {
if (null == digits || digits.length == 0) return digits;
int now = digits.length - 1;
while (now >= 0) {
if (digits[now] != 9) {
digits[now]++;
break;
}
digits[now--] = 0;
}
if (now == -1) {
int[] result = new int[digits.length + 1];
result[0] = 1;
for (int i = 0; i < digits.length; i++) result[i + 1] = digits[i];
return result;
}
return digits;
}
}
0%