File tree Expand file tree Collapse file tree 1 file changed +15
-32
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +15
-32
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
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
- */
22
3
public class _66 {
23
4
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 ;
32
17
}
33
- digits [i ] = 0 ;
18
+ int [] newNumber = new int [len + 1 ];
19
+ newNumber [0 ] = 1 ;
20
+ return newNumber ;
34
21
}
35
- int [] newNumber = new int [len + 1 ];
36
- newNumber [0 ] = 1 ;
37
- return newNumber ;
38
22
}
39
- }
40
23
}
You can’t perform that action at this time.
0 commit comments