1. Description
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
2. Example
digits = [9,9,9]
return [1,0,0,0]
3.Code
import java.util.Arrays;
public class LeetCode0066 {
public int[] plusOne(int[] digits) {
if (digits == null || digits.length == 0) {
return digits;
}
int carry = 1;
for (int i = digits.length - 1; i >= 0; i--) {
carry += digits[i];
digits[i] = carry % 10;
carry /= 10;
}
if (carry == 0) {
return digits;
}
else {
int[] tmp = new int[digits.length + 1];
tmp[0] = carry;
System.arraycopy(digits, 0, tmp, 1, digits.length);
return tmp;
}
}
public static void main(String[] args) {
LeetCode0066 leetcode = new LeetCode0066();
int[] digits = new int[] { 9, 9, 9 };
System.out.println(Arrays.toString(leetcode.plusOne(digits)));
}
}
Comments | NOTHING