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

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

你可以假设除了整数 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. delphi BLE 学习

    TBluetoothLE 控件 TBluetoothLE.FManager: TBluetoothLEManager; class constructor TBluetoothLEManager.Cr ...

  2. linux服务器中Apache隐藏index.php失败

    可以通过URL重写隐藏应用的入口文件index.php,下面是相关服务器的配置参考: [Apache] httpd.conf配置文件中加载了mod_rewrite.so模块 AllowOverride ...

  3. spring是什么

    spring是一个容器,用于降低代码间的耦合度,根据不同的代码采用了ioc和aop这二种技术来解耦合. 比如转账操作:a用户少1000,b用户多1000.这是主业务逻辑   IOC 涉及到的事务,日志 ...

  4. 文件 md5 查看 命令

    Windows命令查看文件MD5   certutil -hashfile yourfilename.ext MD5 certutil -hashfile yourfilename.ext SHA1 ...

  5. Linux实战教学笔记46:NoSQL数据库之redis持久化存储 (二)

    第3章 Redis数据类型详解 3.1 Redis键/值介绍 Redis key值是二进制安全的,这意味着可以用任何二进制序列作为key值,从形如"foo"的简单字符串到一个JPG ...

  6. Spark Streaming原理简析

    执行流程 数据的接收 StreamingContext实例化的时候,需要传入一个SparkContext,然后指定要连接的spark matser url,即连接一个spark engine,用于获得 ...

  7. 143. Reorder List(List)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do th ...

  8. cocoapods使用问题集锦(2017-04)

    今天公司在公司新发的电脑上边安装cocoapod发现容易忘记的几个问题,感觉需要记录下来. 问题一:系统默认ruby镜像的卸载命令行 -->     gem sources --remove h ...

  9. Python操作Excel删除一个Sheet

    在使用Python进行数据分析处理,操作Excel,有时需要删除某个Excel里的某个sheet,这里记录一个我测试成功的一个办法 软件环境: 1.OS:Win 10 64位 2.Python 3.7 ...

  10. c# 类中使用ResolveUrl

    类中使用ResolveUrl 1>获取当前page然后调用ResolveUrl System.Web.UI.Page page = HttpContext.Current.CurrentHand ...