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.
//題意:一個整數按位存儲於一個int數組中,排列順序為:最高位在digits[0] ,最低位在digits[n-1], //例如:98,存儲為:digits[0]=9; digits[1]=8; //解題思路:從數組的最後一位開始加1,需要考慮進位,如果到digits[0]位之後仍然有進位存在,則將進位加上 class Solution { public: vectorplusOne(vector &digits) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int a = 1; vector ans; vector ::iterator it; for(int i = digits.size() - 1;i >= 0;i--) { it = ans.begin(); int tmp = (a + digits[i]) % 10; a = (a + digits[i]) / 10; ans.insert(it, tmp); } if(a != 0) { it = ans.begin(); ans.insert(it, a); } return ans; } };