您的位置:首页 > 其它

leetcode 刷题之路 75 Plus One

2014-08-10 19:25 435 查看
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.

用vector表示一个数,对这个数进行加1操作。

AC code:

class Solution {
public:
    vector<int> plusOne(vector<int> &digits) 
    {
        int sum=1,n=digits.size();
        for(int i=n-1;i>=0;i--)
        {
            digits[i]=(digits[i]+1);
            if(digits[i]<10)
                break;
            else
            {
                digits[i]%=10;
                if(i==0)
                    digits.insert(digits.begin(),1);
            }
        }
        return digits;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: