问题描述:
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 class Solution { 2 public: 3 vector<int> plusOne(vector<int> &digits) { 4 int l=digits.size(); 5 if(l==0){ 6 return digits; 7 } 8 if(l==1){ 9 if(digits[0]!=9){ 10 digits[0]++; 11 return digits; 12 } 13 else{ 14 vector<int> v; 15 v.push_back(1); 16 v.push_back(0); 17 return v; 18 } 19 } 20 int flg=0; 21 int temp=digits[l-1]+1; 22 if(temp>9){ 23 digits[l-1]=0; 24 flg=1; 25 } 26 else{ 27 digits[l-1]=temp; 28 return digits; 29 } 30 for(int i=l-2;i>=0;i--){ 31 int temp=digits[i]+1; 32 if(temp>9){ 33 digits[i]=0; 34 flg=1; 35 } 36 else{ 37 digits[i]=temp; 38 return digits; 39 } 40 } 41 if(flg){ 42 //vector<int>::iterator it=; 43 digits.insert(digits.begin(),1); 44 } 45 return digits; 46 } 47 };