A clear C++ approach. 4ms.


  • 1
    Y
    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
        	if(!digits.size()){
        		digits.insert(digits.end(),1);
                return digits;
        	}
        	vector<int>::iterator it=digits.end();
        	it--;(*it)++;
        	while(*it>9){
        		if(it==digits.begin()){*it=0;digits.insert(it,1);break;}
        		*it=0;it--;(*it)++;
        	}
        	return digits;
        }
    };

Log in to reply
 

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