Leetcode 66 Plus One
來源:程序員人生 發(fā)布時(shí)間:2016-11-21 09:06:09 閱讀次數(shù):2471次
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.
摹擬大數(shù)加法加1,
注意判斷首位是不是有進(jìn)位!
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;
}
};
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈