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.
题目比较简单,直接上代码:
vector<int> plusOne(vector<int>& digits) { int cflag=1;; for(int i =digits.size()-1;i>=0 && cflag>0;i--) { if(digits[i]+cflag >= 10) { digits[i]=0; cflag=1; } else { digits[i]=digits[i]+cflag; cflag=0; } } if(cflag==1) digits.insert(digits.begin(),cflag); return digits; }
时间: 2024-10-20 05:19:20