// Source : https://oj.leetcode.com/problems/plus-one/ // Author : Hao Chen // Date : 2014-06-21 /********************************************************************************** * * 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. * **********************************************************************************/ #include #include using namespace std; vector plusOne(vector &digits) { int carry=1; vector v; while(digits.size()>0){ int x = digits.back(); digits.pop_back(); x = x + carry; v.insert(v.begin(), x%10); carry = x/10; } if (carry>0){ v.insert(v.begin(), carry); } return v; } void printVector(vector& v) { cout << "{ "; for(int i=0; i d(&a[0], &a[0]+sizeof(a)/sizeof(int)); vector v = plusOne(d); printVector(v); return 0; }