File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
July Challenge/com/leetcode/JulyChallenge/week1 Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .leetcode .JulyChallenge .week1 ;
2+
3+ /*
4+ * Time complexity :- O(n)
5+ * Space complexity :- O(1)
6+ *
7+ *
8+ */
9+
10+ public class PlusOne {
11+
12+ public int [] plusOne (int [] digits ) {
13+ int n = digits .length ;
14+
15+ for (int i = n - 1 ; i >= 0 ; i --) {
16+ if (digits [i ] < 9 ) {
17+ digits [i ]++;
18+ return digits ;
19+ }
20+ digits [i ] = 0 ;
21+ }
22+
23+ int edgeCase [] = new int [n + 1 ];
24+ edgeCase [0 ] = 1 ;
25+ return edgeCase ;
26+
27+ }
28+
29+ public static void main (String [] args ) {
30+ int digits [] = { 4 , 3 , 2 , 1 };
31+ int digits1 [] = { 9 , 9 , 9 , 9 };
32+ int digits2 [] = { 1 , 2 , 3 , 9 };
33+
34+ int result [] = new PlusOne ().plusOne (digits );
35+ int result1 [] = new PlusOne ().plusOne (digits1 );
36+ int result2 [] = new PlusOne ().plusOne (digits2 );
37+
38+ print (result );
39+ print (result1 );
40+ print (result2 );
41+ System .out .println ();
42+
43+ }
44+
45+ private static void print (int [] result ) {
46+ for (int i : result )
47+ System .out .print (i + " " );
48+
49+ System .out .println ();
50+ }
51+
52+ }
You can’t perform that action at this time.
0 commit comments