forked from leetcoders/LeetCode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusOne.java
More file actions
28 lines (26 loc) · 777 Bytes
/
Copy pathPlusOne.java
File metadata and controls
28 lines (26 loc) · 777 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
/*
Author: King, wangjingui@outlook.com
Date: Dec 25, 2014
Problem: Plus One
Difficulty: Easy
Source: https://oj.leetcode.com/problems/plus-one/
Notes:
Given a number represented as an array of digits, plus one to the number.
Solution: ...
*/
public class Solution {
public int[] plusOne(int[] digits) {
if (digits.length == 0) return digits;
int carry = 1;
for (int i = digits.length - 1; i >= 0; --i) {
digits[i] += carry;
carry = digits[i] / 10;
digits[i] = digits[i] % 10;
}
if (carry == 0) return digits;
int[] res = new int[digits.length + 1];
res[0] = carry;
System.arraycopy(digits, 0, res, 1, digits.length);
return res;
}
}