一、题目叙述:
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.
Subscribe to see which companies asked this question.
二、解题思路:
非常简单的题,考虑完整即可。
三、源码:
public class Solution
{
public int[] plusOne(int[] digits)
{
int size = digits.length;
// int[] result = new int[size + 1];
int sum = 0;
for (int i = size - 1; i >= 0; i--)
{
sum = digits[i] + 1;
if (sum == 10) {digits[i] = 0; continue;}
else {digits[i] = sum; break;}
}
if (sum == 10)
{
int[] result = new int[size + 1];
for (int j = 0; j < size; j++)
result[j + 1] = digits[j];
result[0] = 1;
return result;
}
else
return digits;
}
public static void main(String args[])
{
int[] digits = {0};
Solution solution = new Solution();
int[] abc = solution.plusOne(digits);
for(int i = 0; i < abc.length; i ++)
System.out.print(abc[i]);
}
}