Skip to content

Commit e068f1a

Browse files
author
applewjg
committed
Plus ONe
Change-Id: I3d9f1a742c99bf4415dae6d14dd540041f31280a
1 parent de567df commit e068f1a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

PlusOne.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Author: King, [email protected]
3+
Date: Dec 25, 2014
4+
Problem: Plus One
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/plus-one/
7+
Notes:
8+
Given a number represented as an array of digits, plus one to the number.
9+
10+
Solution: ...
11+
*/
12+
13+
public class Solution {
14+
public int[] plusOne(int[] digits) {
15+
if (digits.length == 0) return digits;
16+
int carry = 1;
17+
for (int i = digits.length - 1; i >= 0; --i) {
18+
digits[i] += carry;
19+
carry = digits[i] / 10;
20+
digits[i] = digits[i] % 10;
21+
}
22+
if (carry == 0) return digits;
23+
int[] res = new int[digits.length + 1];
24+
res[0] = carry;
25+
System.arraycopy(digits, 0, res, 1, digits.length);
26+
return res;
27+
}
28+
}

0 commit comments

Comments
 (0)