首页 > 其他 > 详细

LeetCode OJ:Plus One (加1)

时间:2015-10-11 23:01:25      阅读:284      评论:0      收藏:0      [点我收藏+]

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就可以,一开始尽然还没看懂题目,真是糗,代码如下:

 1     vector<int> plusOne(vector<int>& digits) {
 2         reverse(digits.begin(), digits.end());
 3         int sz = digits.size();
 4         int flag = 0;
 5         for(int i = 0; i< sz; ++i){
 6             int val = digits[i] + flag;
 7             digits[i] %= val;
 8             flag = val/10;
 9         }
10         if(flag != 10)
11             digits.push_back(flag);
12         reverse(digits.begin(), digits.end());
13         return digits;
14     }

 

LeetCode OJ:Plus One (加1)

原文:http://www.cnblogs.com/-wang-cheng/p/4870154.html

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