博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

LeetCode【66】Plus One

Posted on 2015-04-22 19:06  NUST小文  阅读(109)  评论(0编辑  收藏  举报

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<int> plusOne(vector<int>& digits) {
        int cflag=1;;
        for(int i =digits.size()-1;i>=0 && cflag>0;i--)
        {
            if(digits[i]+cflag >= 10)
            {
                digits[i]=0;
                cflag=1;
            }
            else
            {
                digits[i]=digits[i]+cflag;
                cflag=0;
            }
        }
        if(cflag==1)
            digits.insert(digits.begin(),cflag);
        return digits;
    }