您的位置:首页 > 其它

LeetCode Plus One

2015-11-09 10:47 316 查看
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.

题意是数组存储的是一个数的每位数字,然后把这个数字加1,这跟用字符串存储数字是一个道理,按照加法规则从低位开始处理进位,当最高位有进位的时候就插入一位。
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int i = digits.size()-1;
digits[i]++;
while(i > 0){
if(digits[i] >= 10){
digits[i-1]++;
digits[i] -= 10;
}
i--;
}
if(digits[0] >= 10){
digits[0] -= 10;
digits.insert(digits.begin(), 1);
}
return digits;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: