LeetCode:Plus One
Given a number represented as an array of digits, plus one to the number
分析:too simple 本文地址
class Solution { public: vector<int> plusOne(vector<int> &digits) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int len = digits.size(); vector<int> res(digits); int carrybit = 1, i; for(i = len-1; i >= 0; i--) { if(carrybit == 0)break; else { res[i] += carrybit; if(res[i] >= 10) { carrybit = 1; res[i] -= 10; } else carrybit = 0; } } if(i < 0 && carrybit == 1) res.insert(res.begin(), carrybit); return res; } };
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3475284.html