标签:
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
class Solution { public: static vector<int> plusOne(vector<int>& digits) { int i = digits.size(); while (i > 0 && digits[--i] == 9) //如果末尾的值为9;且不为首元素时 { digits[i] = 0; if (i == 0) //如果9已经是首元素了,则插入一个0 { digits.insert(digits.begin(), 0); } } digits[i] += 1; //在最前面一个不是9的值+1 return digits; } };
标签:
原文地址:http://www.cnblogs.com/dylqt/p/4903426.html