Plus One
2015-07-08 22:32
561 查看
Question:
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.
Solution:
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.
Solution:
class Solution { public: vector<int> plusOne(vector<int>& digits) { reverse(digits.begin(),digits.end()); int t=digits[0]+1; int val=t%10; int up=t/10; *(digits.begin())=val; for(auto iter=digits.begin()+1;iter!=digits.end();iter++) { t=*iter+up; val=t%10; up=t/10; *iter=val; } if(up) digits.push_back(up); reverse(digits.begin(),digits.end()); return digits; } };
相关文章推荐
- Remove Duplicates from Sorted List II
- WinRT中如何快速获取Uri地址中的QueryString键值对信息
- 7.8
- 《番茄工作法图解》第三章读书笔记
- Android入门(37)——第十四章 使用SeekBar制作可拖动的进度条
- Xamarin.Android开发实践(十四)
- Rotate Image
- Spring事务管理
- UI的Title
- Spark 通讯模块 以及常用分布式通讯方式
- objective c 方法快速查询手册
- hibernate优缺点
- Spark 通讯模块 以及常用分布式通讯方式 分类: spark 2015-07-08 22:29 23人阅读 评论(0) 收藏
- Spring中使用Hibernate
- POJ 1003:Hangover
- Mutex Objects
- 程序启动的完整过程
- Trapping Rain Water
- POJ 1003:Hangover
- 大学生的最佳顾主访问节目感悟