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,再反转回去。
1 class Solution { 2 public: 3 vector<int> plusOne(vector<int> &digits) { 4 vector<int> res=digits; 5 reverse(res.begin(),res.end()); 6 7 int sum=0; 8 for(int i=0;i<res.size();i++) 9 { 10 sum=(res[i]+1)/10; 11 int re=(res[i]+1)%10; 12 res[i]=re; 13 if(sum==0)break; 14 } 15 if(sum!=0)res.push_back(sum); 16 reverse(res.begin(),res.end()); 17 18 return res; 19 } 20 };
原文:http://www.cnblogs.com/hicandyman/p/3825372.html