谢谢你的回答!I still have two questions: 1. Is there a special reason that you want to create this function with this signature? since you will modify the input vector argument of this function , why you still want to return a vector as well? 2. I think we need check the overflow, because the result array still need to represent a valid number. The guy who interviewed me said the function should be able to indicate (i.e. return a flag) if overflow happened.
this function You don't have to return a vector, in fact, you can just return void. I was using my code on leetcode oj as an example. The guy was wrong about checking overflow. Ever heard of big int?
【在 r**d 的大作中提到】 : : 谢谢你的回答!I still have two questions: : 1. Is there a special reason that you want to create this function with this : signature? since you will modify the input vector argument of this function : , why you still want to return a vector as well? : 2. I think we need check the overflow, because the result array still need : to represent a valid number. The guy who interviewed me said the function : should be able to indicate (i.e. return a flag) if overflow happened.
w*e
9 楼
java的版本 供参考一下.. public class Solution { public int[] plusOne(int[] digits) { for(int i = digits.length-1; i>=0; i--){ if(digits[i] != 9){ digits[i]++; return digits; } else{ digits[i] = 0; } }
int[] re = new int[digits.length+1]; re[0] = 1;
return re; } }
w*r
10 楼
public int[] PlusOne(int[] digits){ int carry =1,sum=0; int[] result = new int[digits.length]; for(int i = digits.length -1; i>=0; i--){ sum = carry+digits[i]; carry = sum/10; result[i] = sum%10; } if(carry == 1){ int[] plusone = new int[digits.length+1]; plusone[0] = carry; for(int i=1;iplusone[i] = result[i-1]; return plusone; }else{ return result; } }