程序员人生 网站导航

Leetcode 66 Plus One

栏目:php教程时间:2016-11-21 09:06:09

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; } };


------分隔线----------------------------
------分隔线----------------------------

最新技术推荐