前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。
博客链接:mcf171的博客
——————————————————————————————
The digits are stored such that the most significant digit is at the head of the list.
这个题目没啥好说的。。Your runtime beats 40.01% of java submissions.
public class Solution {
public int[] plusOne(int[] digits) {
if(digits.length == 0) return digits;
int carryNumber = 1;
for(int i = digits.length - 1; i >= 0;i --){
int sum = digits[i] + carryNumber;
digits[i] = sum % 10;
carryNumber = sum / 10;
if(carryNumber == 0) break;
}
if(carryNumber == 1){
int[] newArray = new int[digits.length + 1];
newArray[0] = 1;
for(int i = 0; i < digits.length; i ++) newArray[i+1] = digits[i];
digits = newArray;
}
return digits;
}
}
——————————————————————————————