File tree 2 files changed +46
-0
lines changed
LeetCode/Array/66. Plus One
2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ Given a non-empty array of digits representing a non-negative integer, increment one to the integer.
2
+
3
+ The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.
4
+
5
+ You may assume the integer does not contain any leading zero, except the number 0 itself.
6
+
7
+ Example 1:
8
+
9
+ Input: [1,2,3]
10
+ Output: [1,2,4]
11
+ Explanation: The array represents the integer 123.
12
+ Example 2:
13
+
14
+ Input: [4,3,2,1]
15
+ Output: [4,3,2,2]
16
+ Explanation: The array represents the integer 4321.
Original file line number Diff line number Diff line change
1
+ /**
2
+
3
+ @lc id : 66
4
+ @problem : Plus One
5
+ @author : github.com/rohitkumar-rk
6
+ @date : 06/07/2020
7
+ @url : https://leetcode.com/problems/plus-one/
8
+ @difficulty : easy
9
+ */
10
+
11
+ class Solution {
12
+
13
+ public int [] plusOne (int [] digits ) {
14
+
15
+ for (int i = digits .length -1 ; i >= 0 ; i --){
16
+ if (digits [i ] < 9 ){
17
+ digits [i ]++;
18
+ return digits ;
19
+ }
20
+
21
+ digits [i ] = 0 ;
22
+ }
23
+
24
+ //If carry has propogated to the end,
25
+ //First index will contain 1 and all other 0
26
+ int res [] = new int [digits .length +1 ];
27
+ res [0 ] = 1 ;
28
+ return res ;
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments