Plus One

easy 原题链接:https://leetcode.com/problems/plus-one/

Plus One

原题链接:https://leetcode.com/problems/plus-one/arrow-up-right

描述

Given a non-empty array of digits representing a non-negative integer, increment one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

例子

Input: digits = [1,2,3]

Output: [1,2,4]

Explanation: The array represents the integer 123.

解法一

dequearrow-up-right

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int n = digits.size();
        deque<int> dig;
        dig.assign(digits.begin(),digits.end());
	    for (int i = n - 1; i >= 0; --i)
	    {
		    if (dig[i] == 9)
		    {
			    dig[i] = 0;
                if(i==0){
                    dig.push_front(1);
                }
            }else{
                dig[i]++;
                break;
            }
	    }
        vector<int>res;
        res.assign(dig.begin(),dig.end());
        return res;
    }
};

解法二

解法三

insertarrow-up-right

最后更新于