您的位置:首页 > 其它

Plus One

2015-12-07 10:53 351 查看
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.

class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int q = 1;
for(int i=digits.size()-1;i>=0;i--){
int temp = (digits[i]+q)/10;
digits[i] = (digits[i]+q)%10;
q = temp;
}

if(q == 0){
return digits;
}else{
vector<int> result(digits.size()+1);
result[0] = q;
for(int i=1;i<result.size();i++){
result[i] = digits[i-1];
}
return result;
}

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: