首页 > 代码库 > LeetCode--Plus One

LeetCode--Plus One

考查,最高位有进位

 1 class Solution { 2 public: 3     vector<int> plusOne(vector<int> &digits) { 4         // IMPORTANT: Please reset any member data you declared, as 5         // the same Solution instance will be reused for each test case. 6         int i; 7         int n=digits.size(); 8         if(n==0) 9             return digits;10         int mod=1;11         for(i=n-1;i>=0;--i)12         {13             digits[i]+=mod;14             mod=digits[i]/10;15             digits[i]=digits[i]%10;16             17         }18         if(mod!=0)19             digits.insert(digits.begin(),mod);20         return digits;21     }22 };

 

LeetCode--Plus One