首页 > 其他 > 详细

Plus One

时间:2015-06-17 00:24:15      阅读:238      评论:0      收藏:0      [点我收藏+]

Description:

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.

Code:

 1 vector<int> plusOne(vector<int>& digits) {
 2        
 3         vector<int>result(digits);
 4         bool flag = true;
 5         
 6         int index = result.size()-1;
 7         while ( index >= 0 )
 8         {
 9             result[index] = (result[index]+1)%10;
10             flag = (result[index--]==0)?true:false;
11             
12             if (!flag)
13                 return result;
14         }
15         
16         if (flag)
17         {
18             result.insert(result.begin(),1);
19         }
20         return result;
21     }

 

Plus One

原文:http://www.cnblogs.com/happygirl-zjj/p/4582163.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!