C++ intuitive method by stack


  • 0
    class Solution {
    public:
    	vector<int> plusOne(vector<int>& digits) {
    		vector<int> ans;
    		vector<int> num = digits;
    		int n = num.size();
    		++num[n - 1];
    		for(int i = n - 1; i > 0; --i)
    		{
    			int a = num[i];
    			if(a < 10)
    				break;
    			else
    			{
    				num[i] = 0;
    				++num[i - 1];
    			}
    		}
    		
    		int bit = 0;
    		while(bit < n)
    		{
    			int val = num[bit];
    			if(val == 10)
    			{
    				ans.push_back(1);
    				ans.push_back(0);
    			}
    			else
    			{
    				ans.push_back(val);
    			}
    			++bit;
    		}
    
    		return ans;
    	}
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.