您的位置:首页 > 其它

[leetcode.com]算法题目 - Plus One

2013-09-11 22:18 288 查看
Given a number represented as an array of digits, plus one to the number.

class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
bool allNine = true;
int size = digits.size();

for(int i = 0; i< size; i++){
if(digits[i] != 9){
allNine = false;
break;
}
}

if(allNine){
vector<int> result(1+size, 0);
result[0] = 1;
return result;
}

vector<int> result(size);

for(int i=0;i<size;i++){
result[i]=digits[i];
}

result[size-1] += 1;
int k = size-1;

while(10==result[k]){
result[k] = 0;
k--;
result[k]++;
}

return result;
}
};


我的答案
思路:可能出现的意外情况只有数字全是9的时候,这种情况单独拿出来讨论一下,剩下的情况都不可能全为9了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: