-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
35 lines (34 loc) · 817 Bytes
/
Copy pathsolution.java
File metadata and controls
35 lines (34 loc) · 817 Bytes
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
// 66. Plus One
// https://leetcode.com/problems/plus-one/
// Easy | Java | Accepted 2026-01-21
// Runtime 1 ms | Memory 43.6 MB
class Solution {
public int[] plusOne(int[] digits) {
List<Integer> list = new ArrayList<>();
int carry = 1;
for(int i = digits.length-1; i>=0; i--)
{
int sum = digits[i]+carry;
carry = sum/10;
if(i>0)
{
list.add(0, sum%10);
}
else
{
list.add(0, sum%10);
if(carry==1)
{list.add(0, carry);
}
}
}
int[] ans = new int[list.size()];
int j = 0;
for(int n : list)
{
ans[j] = n;
j++;
}
return ans;
}
}