leetcode Plus ONE
2015-10-14 21:29
387 查看
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.
分析:这是一个进位加法运算,设置进位变量add,初始值1,判断最低位加1后是否满10,满10则add=1,否则=0;如此循环。直至结束:
The digits are stored such that the most significant digit is at the head of the list.
分析:这是一个进位加法运算,设置进位变量add,初始值1,判断最低位加1后是否满10,满10则add=1,否则=0;如此循环。直至结束:
vector<int> plusOne(vector<int>& digits) { int i; int sum=0; int add=1; vector<int>a(digits.size(),0); for(i=digits.size()-1;i>=0;i--) { sum=digits[i]+add; add=sum/10; a[i]=sum%10; } if(add==1)a.insert(a.begin(),add); return a; }
相关文章推荐
- Python 遍历目录找出包含指定字符串的文件夹
- [置顶] iOS中UITabBarController的剖析
- 第 三 十 天:Linux 系 统 优 化 扩 展
- 读书一定要掌握正确的阅读方法:读书10
- 前端系统分析
- 第一次个人项目修改
- Python 遍历目录找出包含指定字符串的文件夹
- Failed to resolve base type System.Security.Principal.GenericIdentity for type System.Net.HttpListen
- ListView 禁止Item被点击
- 黑马程序员——反射与其相关概念
- 10004---Trail ~ New Data Model
- MAC安装MYSQL
- lintcode-背包问题II-125
- [c++]使用前后缀表示常量的类型
- 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-03-优化器
- How to prevent object bloat in PostgreSQL
- iOS static dispatch_once_t oneToken;
- java.util.ConcurrentModificationException 解决办法
- Android M 新的运行时权限开发者需要知道的一切
- 博客园收藏