标签:
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: vector<int> plusOne(vector<int> &digits) { bool carry = false; for(int i =digits.size()-1; i>=0;i--) { if(!carry) { if(digits[i] == 9) { carry = true; digits[i] = 0; } else { digits[i]+=1; break; } } else { if(digits[i] == 9) { digits[i] = 0; } else { carry = false; digits[i] +=1; break; } } } if(carry) { digits.push_back(1); reverse(digits.begin(), digits.end()-1); reverse(digits.begin(), digits.end()); } return digits; } };
标签:
原文地址:http://www.cnblogs.com/qionglouyuyu/p/4930144.html