Leetcode66.Plus_One

注意连续进位和首位进位。
时间复杂度:O(N)
C++代码:

class Solution {
public:
	vector<int> plusOne(vector<int>& digits) {
		if (*(digits.end() - 1) != 9)
			*(digits.end() - 1) += 1;
		else
		{
			auto it = digits.end() - 1;
			while (*it == 9)
			{
				if (it == digits.begin())
				{
					*it = 1;
					digits.push_back(0);
					return digits;
				}
				*it = 0;
				it--;
			}
			*it+=1;
		}
		return digits;
	}
};

猜你喜欢

转载自blog.csdn.net/qq_42263831/article/details/82776776