给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组。

最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。

你可以假设除了整数 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 加一的更多相关文章

  1. Leetcode加一 (java、python3)

    加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. Given ...

  2. [LeetCode] Plus One Linked List 链表加一运算

    Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...

  3. [LeetCode] Additive Number 加法数

    Additive number is a positive integer whose digits can form additive sequence. A valid additive sequ ...

  4. [LeetCode] Plus One 加一运算

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  5. LeetCode 66. Plus One(加1)

    Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. Yo ...

  6. [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 ...

  7. [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 ...

  8. [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. ...

  9. LeetCode:加一【66】

    LeetCode:加一[66] 题目描述 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外 ...

随机推荐

  1. 【转】内存耗用:VSS/RSS/PSS/USS

    Terms VSS- Virtual Set Size 虚拟耗用内存(包含共享库占用的内存) RSS- Resident Set Size 实际使用物理内存(包含共享库占用的内存) PSS- Prop ...

  2. Git 版本导致 clone 故障

    问题描述: git clone 报错如下: Initialized empty Git repository in /root/project_php/.git/ error: The request ...

  3. JDBC中,如何动态的设置查询条件

    今天看JDBC,发现有段代码,可以减少重复的编写查询方法,如下: public List<Goddess> query(List<Map<String, Object>& ...

  4. 数据库执行的时候报ORA-01653错误

    查明原因是因为表空间文件到达了32G,因为oracle11g单个表空间大于32G的时候就不会自动在扩展了于是需要增加新的表空间文件,下面是4种解决此问题的方法 Meathod1:给表空间增加数据文件 ...

  5. SPI子系统分析之一:框架

    内核版本:3.9.5 SPI子系统概述: 一个SPI主控制器对应一条SPI总线,当然在系统中有唯一的总线编号. SPI总线上有两类设备: 其一是主控端,通常作为SOC系统的一个子模块出现,很多嵌入式M ...

  6. 数据库连接池--druid

    数据库连接池常用的有:dbcp,c3p0,druid 代码仓库(https://github.com/) package com.huawei.test; import java.sql.Connec ...

  7. 在Yii Framework中利用PHPMailer发送邮件(2011-06-02 14:06:23)

    转载▼ 标签: it 分类: 技术共享 官方扩展链接:http://www.yiiframework.com/extension/mailer/这个扩展配置十分方便,如果有问题的话,可以打开Debug ...

  8. UV mapping

    [UV mapping] UV mapping is the 3D modeling process of making a 2D image representation of a 3D model ...

  9. 欲望都市游戏设计 背景图层和UI图层的设计

  10. winfrom在图片上实现绘制

    在控件加载入图片的基础上进行绘制内容 添加文字 首先就是要确定输入的文字,还有文字的样式. 文字的样式用到了FontDialog控件,获取文字呢,就放个textbox就可以了.如果在输入文字的显示展示 ...