Description

https://oj.leetcode.com/problems/plus-one/

Difficulty: 0.5/5.0 star

Analysis

class Solution {
public:
	vector<int> plusOne(vector<int> &digits) {
		bool toPlus = true;
		for (int i = digits.size()-1; i >= 0 && toPlus; i -- ){
			digits[i] = (digits[i] + 1)%10;
			toPlus = (digits[i] == 0);
		}
		if (toPlus)
			digits.insert(digits.begin(), 1);
		return digits;
	}
};

Comments