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

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

你可以假设除了整数 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. Excel中通过向导方式插入chart

    1.插入图表则主要是操作ChartObject对象和Chart对象. Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet); Workshe ...

  2. myBaits缓存

    转自:https://blog.csdn.net/zhongzh86/article/details/50019511 9. Mybatis 缓存 正如大多数持久层框架一样,MyBatis 同样提供了 ...

  3. tomcat https

    转自 http://11lingxian.iteye.com/blog/1491607 双向认证: 客户端向服务器发送消息,首先把消息用客户端证书加密然后连同时把客户端证书一起发送到服务器端, 服务器 ...

  4. Excel VBA 入门(零)

    本教程所用系统环境: Windows 10 Excel 2013 1. 添加开发工具 打开Excel,依然找到"文件"->"选项"->"自 ...

  5. 前端中this的用法

    this指的是方法下的所有参数 handleDelete(record){ this.XXX.AAA (这个this.XXX指的是handleDelete这个方法的所有参数) (let self = ...

  6. App审核被拒(后台定位被拒,ipv6被拒,广告标示被拒的解决方案)

    ipv6被拒问题描述: 解决方案支持ipv6 1)搭建ipv6 环境,搭建好的ipv6 ,环境会有一个共享wifi, 具体如何搭建ipv6测试环境参考本地如何搭建IPv6环境测试你的APP2)app连 ...

  7. ubuntu 14.04 Clion2016.2 安装激活与安装后添加快捷启动方式

    参考链接:http://www.cnblogs.com/conw/p/5938113.html 下载clion for linux : http://www.jetbrains.com/clion/d ...

  8. fgetc()

    fgetc() 函数从文件指针中读取一个字符.

  9. [SoapUI] JsonPath 语法 与 XPath 对比

    XPath JSONPath Description / $ the root object/element . @ the current object/element / . or [] chil ...

  10. 路飞项目背景,contentType以及django缓存

    昨日回顾: 分页器: 普通分页 # 普通分页 from rest_framework.pagination import PageNumberPagination -每页的大小(默认) -查询的时候, ...