Given a non-negative number represented as an array of digits, plus one to the number.
The digits is stored such, the most significant digit was at the head of the list.
The large number addition, which was initially thought to be simply an integer addition, was later discovered to be a large number
1 classSolution {2 Public:3vector<int> PlusOne (vector<int>&Digit) {4 //int len = Digit.size ();5 //Long Long res = 0;6 //Reverse (Digit.begin (), Digit.end ());7 //For (int j = len-1;j >= 0;--j)8 // {9 //res = res*10+ Digit[j];Ten // } One //Long Long newres = res+1; A //digit.clear (); - //While (newres!=0) - // { the //int a = newres%10; - //Digit.push_back (a); - //newres/=; - // } + //Reverse (Digit.begin (), Digit.end ()); - //return digit; + A intcarry =0; at //Reverse (Digit.begin (), Digit.end ()); -vector<int>Newvec; - intLen =digit.size (); - for(inti=len-1; i>=0;--i) - { - inttmp; in if(i = = len-1) - { toTMP = Digit[i] +1; + } - Else theTMP = Digit[i] +carry; * $carry =0;Panax Notoginseng - if(tmp>=Ten) the { +carry =1; Atmp=tmp%Ten; the Newvec.push_back (TMP); + } - Else $ { $ Newvec.push_back (TMP); - } - the if(i = =0&& carry = =1) - {Wuyi Newvec.push_back (carry); the } - } Wu Reverse (Newvec.begin (), Newvec.end ()); - returnNewvec; About } $};
(Leetcode) Plus One