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.
题意:给出一个非负的数,用数组来表示这个数,比如说9999就是[9,9,9,9]当对这个数加一的时候,将这个数用数组的形式返回。
public class Solution {
public int[] PlusOne(int[] digits) {
int plus = 1;
List<int> resultList = new List<int>();
for (int i = digits.Length - 1; i >= 0; i--) {
if (plus > 0) {
digits[i] += 1;
if (digits[i] >= 10) {
digits[i] = digits[i] % 10;
} else {
plus--;
}
}
resultList.Add(digits[i]);
if (i == 0 && plus == 1) {
resultList.Add(1);
}
}
resultList.Reverse();
return resultList.ToArray();
}
}