66. Plus One
来源:互联网 发布:mac 恢复删除文件 编辑:程序博客网 时间:2024/06/05 15: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的这个情况,加1后就要进位,最高位变0进一 相当于最高位变1,再在末尾插入1个地位0
有其中1位<9的话,加一,直接返回该数组
C++ AC代码:时间o(n)
class Solution {public: vector<int> plusOne(vector<int>& digits) { int index = digits.size()-1; while(index>0){ if(digits[index]==9){ digits[index--] = 0; }else{ digits[index--]++; return digits; } } if(digits[index]==9){ digits[index] = 1; digits.push_back(0); }else{ digits[index]++; } 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
- 原子操作的实现原理
- ural 1820. Ural Steaks 贪心
- 分布式事务:不过是在一致性、吞吐量和复杂度之间_做一个选择
- 枚举的使用及优势
- PHP数组交集与差集函数
- 66. Plus One
- git 分支合并和删除
- vagrant+虚拟机搭建开发环境
- 解决mysql数据库连接超时问题
- HttpSession
- git 分支使用原则
- linux kernel 为什么需要initrd
- Ubuntu 创建androidstudio快捷图标到桌面
- Android自定义控件之自定义下载progress