leetcode 加一
给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组。
最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。
你可以假设除了整数 0 之外,这个整数不会以零开头。
示例 1:
输入: [1,2,3]
输出: [1,2,4]
解释: 输入数组表示数字 123。
我一看,想居然有这么简单的题,把数组转成数字+1再转会数组不就行了
然而不通过,大概是发生了值超出数字上限之类的错误吧,果然还得思考
想了老半天
/**
* @param {number[]} digits
* @return {number[]}
*/
var plusOne = function (digits) {
function plus(index) {
if (index < 0) {
digits.unshift(1);
return;
}
digits[index] += 1;
if (digits[index] === 10) {
digits[index] = 0;
plus(index - 1)
}
}
plus(digits.length - 1);
return digits;
};
大佬的想法就是一手简单明了,然而并不容易想通
var plusOne = function(digits) {
var len = digits.length;
for (var i = len - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
return [1, ...digits];
};
leetcode 加一的更多相关文章
- Leetcode加一 (java、python3)
加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. Given ...
- [LeetCode] Plus One Linked List 链表加一运算
Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...
- [LeetCode] Additive Number 加法数
Additive number is a positive integer whose digits can form additive sequence. A valid additive sequ ...
- [LeetCode] Plus One 加一运算
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- LeetCode 66. Plus One(加1)
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. Yo ...
- [LeetCode] Largest Plus Sign 最大的加型符号
In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given lis ...
- [LeetCode] Bold Words in String 字符串中的加粗单词
Given a set of keywords words and a string S, make all appearances of all keywords in S bold. Any le ...
- [leetcode]364. Nested List Weight Sum II嵌套列表加权和II
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- LeetCode:加一【66】
LeetCode:加一[66] 题目描述 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外 ...
随机推荐
- leetcode384
public class Solution { private int[] nums; private Random random; public Solution(int[] nums) { thi ...
- Linux监控和安全运维 1.9 zabbix增加客户端监控
1.在客户端安装 zabbix20与服务器相对应. yum install zabbix20-agent 2.修改配置文件 vim /etc/zabbix_agentd.conf Server=127 ...
- 【转】arm和x86的区别
来自: https://blog.csdn.net/u012513972/article/details/78349192/ 信不信,随便逮住一个人问他知不知道CPU,我想他的答案一定会是肯定的,但是 ...
- Java中异常体系
Java中的异常体系 Throwable,是一个类,表示可抛出的,它是所有的异常都继承的类. Throwable的子类,又分为两种,一种是Error,另一种是Exception. Error一般不是应 ...
- 深度学习篇——Tensorflow配置(傻瓜安装模式)
前言 如果你是一个完美主义者,那么请绕过此文,请参考<深度学习篇——Tensorflow配置(完美主义模式)> 安装 pip install tensorflow ok,只要不报错,安装就 ...
- 数组和集合(三):Set集合的使用总结
一.概述 · 继承collection接口 · 无序(不记录添加顺序).不允许元素重复.只允许存在一个null元素 二.实现类 1. HashSet · 底层其实是包装了一个HashMap实现的 · ...
- ascii编码转utf8编码,适用于python2
def ascii2utf8(ascii): line = eval(("'" + ascii.strip() + "'")) return line
- HQL多表查询
------------------siwuxie095 HQL 多表查询 以客户和联系人为例(一对多) 1.内连接 (1)hql 语句写法 from Customer c inner join c. ...
- 第二话:javascript中闭包的理解
闭包是什么? 通过闭包,子函数得以访问父函数的上下文环境,即使父函数已经结束执行. OK,我来简单叙述下,先上图. 都知道函数是javascript整个世界,对象是函数,方法是函数,并且js中实质性的 ...
- java 线程的几个注解
Java并发编程中,用到了一些专门为并发编程准备的 Annotation. 主要包括三类: 类 Annotation(注解) 就像名字一样,这些注解是针对类的.主有要以下三个: @ThreadSafe ...