Simple C++ solution


  • 0
    I

    class Solution {
    public:

    vector<int> plusOne(vector<int>& digits) {
    	int vecSize = digits.size();
    	reverseVector(digits);
    
    	int i = 0;
    	digits.at(i) += 1;
    	while (digits.at(i) == 10)
    	{
    		digits.at(i) = 0;
    		if (i < vecSize - 1)
    		{
    			i++;
    			digits.at(i) += 1;
    		}
    		else
    		{
    			digits.push_back(1);
    			break;
    		}
    	}
    
    	reverseVector(digits);
    
    	return digits;
    }
    
    void reverseVector(vector<int>& digits)
    {
    	int vecSize = digits.size();
    	int halfVecSize = vecSize / 2;
    	int tempDigit;
    	for (int i = 0; i < halfVecSize; ++i)
    	{
    		tempDigit = digits.at(i);
    		digits.at(i) = digits.at(vecSize - 1 - i);
    		digits.at(vecSize - 1 - i) = tempDigit;
    	}
    
    	return;
    }
    

    };


Log in to reply
 

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