66. Plus One
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.
题意:给定一个由0-9组成的数组,这个数组表示一个数,对这个数加一,返回一个新的数组。
public class Solution {
public int[] plusOne(int[] digits) {
int n = digits.length;
for(int i=n-1; i>=0; i--) {
if(digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
int[] newNumber = new int [n+1];
newNumber[0] = 1;
return newNumber;
}
}
参考的答案,解法很简洁明了。
本文介绍了一种将数组表示的非负整数加一的方法,通过遍历数组从末尾开始,处理每一位数字,最终形成加一后的数组。
245

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



