Skip to content

Commit 781fbb7

Browse files
solves plus one
1 parent 2af32eb commit 781fbb7

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
| 35 | [Search Inserted Position](https://leetcode.com/problems/search-insert-position/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/SearchInsertPosition.java) |
2626
| 38 | [Count and Say](https://leetcode.com/problems/count-and-say) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/CountAndSay.java) |
2727
| 53 | [Maximum SubArray](https://leetcode.com/problems/maximum-subarray) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/MaximumSubArray.java) |
28-
| 58 | [Length of Last Word](https://leetcode.com/problems/length-of-last-word) | Easy | |
28+
| 58 | [Length of Last Word](https://leetcode.com/problems/length-of-last-word) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/LengthOfLastWord.java) |
2929
| 66 | [Plus One](https://leetcode.com/problems/plus-one) | Easy | |
3030
| 67 | [Add Binary](https://leetcode.com/problems/add-binary) | Easy | |
3131
| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx) | Easy | |

Diff for: src/PlusOne.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
import java.util.Stack;
4+
5+
public class PlusOne {
6+
public static int[] plusOne(int[] digits) {
7+
Stack<Integer> numbers = new Stack<>();
8+
int carry = 0;
9+
for (int index = digits.length - 1, factor = 1 ; index >= 0 ; index--) {
10+
int value = digits[index] + factor + carry;
11+
numbers.push(value % 10);
12+
carry = value / 10;
13+
factor = 0;
14+
}
15+
if (carry > 0) {
16+
numbers.push(carry);
17+
}
18+
19+
int[] result = new int[numbers.size()];
20+
int index = 0;
21+
while (!numbers.isEmpty()) {
22+
result[index++] = numbers.pop();
23+
}
24+
return result;
25+
}
26+
}

0 commit comments

Comments
 (0)