‘Plus One’ in LeetCode (Easy)
📌 Problem
https://leetcode.com/problems/plus-one/
📌 Answer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public int[] plusOne(int[] digits) {
for (int i = digits.length - 1; i >= 0; i--) {
if (digits[i] != 9) {
digits[i]++;
break;
} else {
digits[i] = 0;
}
}
if (digits[0] == 0) {
int[] res = new int[digits.length + 1];
res[0] = 1;
return res;
}
return digits;
}
}
Comments powered by Disqus.