66. Plus One
来源:互联网 发布:凡科建站怎么样 知乎 编辑:程序博客网 时间:2024/06/05 10:32
题面:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
题解:用一个数组表示一个数,最高位在前面,求写一个函数使其加一后继续可以表示。由于只有当所有位数全是9的时候,数组才会扩长一位,只要有一位不是9,即可将那位加一,是9的变成0 即可。
代码:
class Solution {public: vector<int> plusOne(vector<int>& digits) { for(int i=digits.size()-1;i>=0;i--){ if(digits[i]==9) digits[i]=0; else { digits[i]++; return digits; } } digits[0]=1; digits.push_back(0); return digits; }};
阅读全文
0 0
- [LeetCode]66.Plus One
- LeetCode --- 66. Plus One
- [Leetcode] 66. Plus One
- [leetcode] 66.Plus One
- 66. Plus One
- 66. Plus One
- [leetcode] 66. Plus One
- Leetcode-66. Plus One
- 【leetcode】66. Plus One
- 66. Plus One
- 66. Plus One LeetCode
- 66. Plus One
- 66. Plus One
- 66. Plus One
- 66. Plus One
- 66. Plus One
- 【LeetCode】66. Plus One
- [LeetCode]66. Plus One
- Selenium 文档标注
- Win7和XP操作系统显示文件扩展名
- Contect、SharedPreferences及Intent学习小记
- bootstrap学习27---面板
- 基于Nginx实现10万+并发,你应该做的Linux内核优化
- 66. Plus One
- fastboot 常用命令
- 按回车键,焦点转到下一个
- Spring本质系列(2)-AOP
- 基于java 使用http-post方式请求https webservice接口
- mybatis返回map类型数据空值字段不显示
- cf 的gym里的题目2016-2017 CT S03E03:
- 马氏过程
- extjs批量导入