Leetcode 66 Plus One
來源:程序員人生 發布時間:2016-11-21 09:06:09 閱讀次數:2481次
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.
摹擬大數加法加1,
注意判斷首位是不是有進位!
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int add=1;
for(int i=digits.size()⑴;i>=0;i--)
{
digits[i]=(digits[i]+add)%10;
if(digits[i]!=0) break;
}
if(digits[0]==0) digits.insert(digits.begin(),1);
return digits;
}
};
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈