题目链接:plus-one
import java.util.Arrays;
/**
*
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
*
*/
public class PlusOne {
// 108 / 108 test cases passed.
// Status: Accepted
// Runtime: 196 ms
// Submitted: 0 minutes ago
public int[] plusOne(int[] digits) {
int[] result = new int[digits.length + 1];
result[digits.length] = 1;
for (int i = digits.length - 1; i >= 0; i--) {
result[i] = (digits[i] + result[i + 1]) / 10;
result[i + 1] = (digits[i] + result[i + 1]) % 10;
}
return (result[0] == 0) ? Arrays.copyOfRange(result, 1, result.length) : result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
541




被折叠的 条评论
为什么被折叠?



