您的位置:首页 > 其它

Plus One

2015-09-13 17:28 323 查看
【题目描述】

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 len=digits.size();
        if(digits[len-1]!=9){
            digits[len-1]=digits[len-1]+1;
        }
        else{
            for(int i=len-1;i>=0;i--){
               if(digits[i]==9){
                   digits[i]=0;
               }
               else{
                   digits[i]+=1;
                   break;
               }
            }
        }
        if(digits[0]==0){
            digits.insert(digits.begin(),1);
        }
        return digits;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: