Skip to content

Commit b82a315

Browse files
refactor 66
1 parent 83ce399 commit b82a315

File tree

1 file changed

+15
-32
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+15
-32
lines changed
Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,23 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 66. Plus One
5-
*
6-
* Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
7-
* The digits are stored such that the most significant digit is at the head of the list,
8-
* and each element in the array contain a single digit.
9-
*
10-
* You may assume the integer does not contain any leading zero, except the number 0 itself.
11-
*
12-
* Example 1:
13-
* Input: [1,2,3]
14-
* Output: [1,2,4]
15-
* Explanation: The array represents the integer 123.
16-
*
17-
* Example 2:
18-
* Input: [4,3,2,1]
19-
* Output: [4,3,2,2]
20-
* Explanation: The array represents the integer 4321.
21-
*/
223
public class _66 {
234

24-
public static class Solution1 {
25-
/**credit: https://leetcode.com/problems/plus-one/discuss/24082/My-Simple-Java-Solution*/
26-
public int[] plusOne(int[] digits) {
27-
int len = digits.length;
28-
for (int i = len - 1; i >= 0; i--) {
29-
if (digits[i] < 9) {
30-
digits[i]++;
31-
return digits;
5+
public static class Solution1 {
6+
/**
7+
* credit: https://leetcode.com/problems/plus-one/discuss/24082/My-Simple-Java-Solution
8+
*/
9+
public int[] plusOne(int[] digits) {
10+
int len = digits.length;
11+
for (int i = len - 1; i >= 0; i--) {
12+
if (digits[i] < 9) {
13+
digits[i]++;
14+
return digits;
15+
}
16+
digits[i] = 0;
3217
}
33-
digits[i] = 0;
18+
int[] newNumber = new int[len + 1];
19+
newNumber[0] = 1;
20+
return newNumber;
3421
}
35-
int[] newNumber = new int[len + 1];
36-
newNumber[0] = 1;
37-
return newNumber;
3822
}
39-
}
4023
}

0 commit comments

Comments
 (0)