标签:
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.
这个题目很简单,只要注意进位为1且该为数值为9的情况,以及最后进位为1,同时注意数字的高位存储在向量的低位
class Solution { public: vector<int> plusOne(vector<int>& digits) { vector<int> result; int num = 1; for (int i = digits.size() - 1;i >= 0;--i) { if (1 == num&&digits[i] == 9) { result.insert(result.begin(), 0); num = 1; } else { result.insert(result.begin(), digits[i] + num); num = 0; } } if (1 == num)result.insert(result.begin(), 1); return result; } };
标签:
原文地址:http://www.cnblogs.com/csudanli/p/5877540.html