LeetCode-66
Links:https://leetcode.com/problems/plus-one/
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.
题目大意:给出一个用数组存储的整数,高位在前…例如:123在数组内为[1][2][3]。
返回+1后的值.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Solution { public: vector<int> plusOne(vector<int>& digits) { digits[digits.size()-1]+=1; for(int i=digits.size()-1;i>0;i--) { if(digits[i]>=10) { digits[i-1]+=digits[i]/10; digits[i]=digits[i]%10; } else{ return digits; } } if(digits[0]>=10) { digits.push_back(0); digits[0]=digits[0]/10; } return digits; } }; |
注意进位的情况…(99999+1)…
【LeetCode】66. Plus One