Plus One
来源:互联网 发布:比较好的网红的淘宝店 编辑:程序博客网 时间:2024/05/20 08:28
一、问题描述
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,如果尾数为‘9’,则进1,但有一个小陷阱:我在
if(i == 0){ digits[0] = 0; digits.insert(digits.begin(),1); return digits;
这一步中开始没有想到return,结果调试了很久都没有AC,最后发现这个也是一个退出条件。
三、代码
class Solution {public: vector<int> plusOne(vector<int>& digits) { int i = digits.size() - 1; if(digits[i] == 9){ while(digits[i] == 9){ if(i == 0){ digits[0] = 0; digits.insert(digits.begin(),1); return digits; } else{ digits[i--] = 0; } } digits[i] = digits[i] + 1; } else digits[i] = digits[i] + 1; return digits; }};
0 0
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- Plus One
- 网页验证码 (粗略功能实现)
- 交换机的管理方式及内在机制
- apache模块mod_rpaf ,让nginx后端的apache获取访客IP
- error_reporting() 设置 PHP 的报错级别并返回当前级别
- Adapter模式
- Plus One
- struts2中getOutputStream() has already been called for this response异常解决方法
- Java 基础之(六) final关键字
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- php的empty函数参数不能是一个函数
- 修改CentOs7 上Docker默认文件系统DeviceMapper为OverlayFS
- 函数参数前是否加&的问题
- replicate_do_db和repalicate_wild_do_table跨库同步问题
- Spring4 Bean管理的注解实现