-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
66.java
52 lines (45 loc) · 1.26 KB
/
66.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int[] plusOne(int[] digits) {
int n = digits.length;
for(int i=n-1; i>=0; i--) {
if(digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
int[] newNumber = new int [n+1];
newNumber[0] = 1;
return newNumber;
}
}
__________________________________________________________________________________________________
sample 34596 kb submission
class Solution {
public int[] plusOne(int[] digits) {
HashMap<Integer, Integer> mp=new HashMap<Integer,Integer>();
int j=0,i=0,carry=1,temp=0;
for ( j=digits.length-1;j>=0;j--)
{
temp=((digits[j])+carry)%10;
if((digits[j])+carry==10)
carry=1;
else carry=0;
mp.put(i,temp);
i++;
}
if (carry==1)
mp.put(i,carry);
int[] newarr=new int[mp.size()];
i=0;
for (int p=mp.size()-1;p>=0;p--)
{
newarr[i]=mp.get(p);
i++;
}
return newarr;
}
}
__________________________________________________________________________________________________