leetcode #66 in cpp
来源:互联网 发布:竞彩缩水软件 编辑:程序博客网 时间:2024/06/05 19:35
Solution:
1.start from the tail of the input, and keep adding 1 until we reach the head of the input or the digit + 1 is less than 10.
2.check if the head >= 10. If so insert an extra 1 to the input.
Code:
class Solution {public: vector<int> plusOne(vector<int>& digits) { int i = digits.size() - 1; int carry = 1; while(carry && i >=0){ digits[i]+=carry; if(digits[i] >=10){ carry = 1; digits[i]%=10; }else carry = 0; i--; } if(carry){ digits.insert(digits.begin(), 1); } return digits; }};
0 0
- leetcode #66 in cpp
- leetcode #12 in cpp
- Leetcode #13 in cpp
- Leetcode #14 in cpp
- leetcode %15 in cpp
- leetcode #16 in cpp
- leetcode #17 in cpp
- leetcode #18 in cpp
- leetcode #20 in cpp
- leetcode #21 in cpp
- leetcode #22 in cpp
- leetcode #23 in cpp
- leetcode #24 in cpp
- leetcode #25 in cpp
- leetcode #26 in cpp
- leetcode #27 in cpp
- leetcode #28 in cpp
- leetcode #29 in cpp
- 程序猿随想
- pl/sql触发器案列
- java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
- Qt/C++ 360安全卫士主界面动态按钮demo
- bootstrapvalidator之API学习
- leetcode #66 in cpp
- windows2008SVN安装同步web出错问题
- 修改oracle数据库的编码为utf8
- 【hdu】3231 Box Relations【拓扑排序三维】
- Creational Patterns Part 5/5: Prototype Pattern
- Python Tkinter学习笔记
- MinStack
- php 对象clone
- leetcode #67 in cpp