LeetCode Plus One
2015-11-09 10:47
316 查看
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,这跟用字符串存储数字是一个道理,按照加法规则从低位开始处理进位,当最高位有进位的时候就插入一位。
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 i = digits.size()-1; digits[i]++; while(i > 0){ if(digits[i] >= 10){ digits[i-1]++; digits[i] -= 10; } i--; } if(digits[0] >= 10){ digits[0] -= 10; digits.insert(digits.begin(), 1); } return digits; } };
相关文章推荐
- UnKnownHostException
- oracle的查询结果合并
- vc++6.0快捷键
- 程序员眼中的英语单词
- 你会做Web上的用户登录功能吗?
- 慢慢数据抓取路,从BeautifulSoup走起
- Nginx下让SSL支持SPDY协议
- Paint.Join与Paint.Cap
- java源码分析03-InputSteam
- Spring data redis-StringRedisTemplate 用法
- 程序设计——规则
- Kafka笔记--监控系统KafkaOffsetMonitor
- Redis操作的封装类
- 匈牙利算法—解决二分图最大匹配问题
- 侧边导航(高级搜索)应用到 $("xxxxx").click(function () {}点击某控件是触发的方法,
- mysql 下 计算 两点 经纬度 之间的距离
- UI界面-帧布局
- iOS成长之路-关于推送
- 深入浅出MIPS 〇 序章
- Android webview