国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > Leetcode 66 Plus One

Leetcode 66 Plus One

来源:程序员人生   发布时间:2016-11-21 09:06:09 阅读次数:2116次

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


生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生