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

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

你可以假设除了整数 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. grideh SelectedRows Bookmark

    VCL grideh 选中多行 TBookmark.Bookmark.GotoBookmark TBookmark bm= DataSet->GetBookmark(); DataSet-> ...

  2. 解读linux中用户密码规则及忘记root口令的破解(思路)

    linux当中,用户名和密码表对应关系放在/etc/passwd中,如: root:x:0:0:root:/root:/bin/bash 格式代表意义分别为 用户名:密码:用户id:组id:用户描述 ...

  3. 【转】tcp_tw_recycle和tcp_timestamps导致connect失败问题

    (2012-02-01 18:40:32)     近来线上陆续出现了一些connect失败的问题,经过分析试验,最终确认和proc参数tcp_tw_recycle/tcp_timestamps相关: ...

  4. C语言增量内存申请 realloc

    void* realloc (void* ptr, size_t size); Reallocate memory block Changes the size of the memory block ...

  5. Android5.0新动画之VectorDrawable

    SVG是前端的一套标准,Vector是在Android中使用,他只是实现了SVG语言的Path的标签 Vector的常用语法   M = moveto(M X,Y): 将画笔移动到指定的坐标位置   ...

  6. Promethus安装指南

    由于Prometheus是go语言写的,所以不需要编译,安装的过程非常简单,仅需要解压然后运行.Prometheus官方下载地址:https://prometheus.io/download/ 安装P ...

  7. System.Diagnostics.Conditional

    [System.Diagnostics.Conditional] 指示编译器当特定的宏定义了时,才生成此方法的相应代码.只能应用于AttributeClass.Method. 参考:http://ms ...

  8. 解决SharePoint下载文件时自动修改扩展名的问题

    今天,有人告诉了我一个有趣的问题.当用户将一个扩展名为.ai的文件(Adobe illustrator格式的文件)上载到SharePoint 2013文档库中之后,点击它下载时,下载提示栏所显示的文件 ...

  9. 145. Binary Tree Postorder Traversal (Stack, Tree)

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  10. adf错误

    1>无法验证事务处理中的所有行 运行项目报错: javax.faces.el.EvaluationException: oracle.jbo.TxnValException: JBO-27023 ...