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

You may assume the integer do not contain any leading zero, except the number 0 itself.

The digits are stored such that the most significant digit is at the head of the list.


题目标签:Array

  这道题目给了我们一个digits 的array, 这个array 等于一个数字,让我们加1。来分析一下,如果是不需要进位的 <9 , 那么直接加1就返回。如果是需要进位的,=9, 那么我们需要把目前的digit 改为0,接着继续检查前一位,因为你不确定前一位加1 是不是需要进位。一旦当我们找到不需要进位的那个digit, 加1 返回就可以了。如果碰到一种极端的情况,类似于 999 的话,那么我们 遍历完之后 是 000, 还需要一个1 ,所以要重新建立一个array, size + 1,把1 加在 [0]的位置。

Java Solution:

Runtime beats 39.20%

完成日期:04/05/2017

关键词:Array

关键点:特殊情况类似于999 需要重新建立一个array with size + 1 来放1在最左边

 class Solution
{
public int[] plusOne(int[] digits)
{
int[] res; // iterate digits from right to left
for(int i= digits.length - 1; i>=0; i--)
{
if(digits[i] == 9) // digit = 9
{
digits[i] = 0;
}
else // digit from 0 to 8
{
digits[i]++;
return digits; // after add 1 to digit, return
} } // coming here means the 1 is not yet added and also digits is finished
// there is only one case: 999...
res = new int[digits.length + 1];
res[0] = 1; return res;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 66. Plus One(加1)的更多相关文章

  1. [LeetCode] 66. Plus One 加一

    Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...

  2. [leetcode]66. Plus One加一

    Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...

  3. 前端与算法 leetcode 66. 加一

    目录 # 前端与算法 leetcode 66. 加一 题目描述 概要 提示 解析 解法一 解法二 算法 # 前端与算法 leetcode 66. 加一 题目描述 给定一个由整数组成的非空数组所表示的非 ...

  4. 每日一道 LeetCode (14):数组加一

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  5. Java实现 LeetCode 66 加一

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

  6. [LeetCode]66. 加一(数组)

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

  7. python(leetcode)-66加一问题

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

  8. LeetCode(66): 加一

    Easy! 题目描述: 给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会 ...

  9. Leetcode——66.加一

    @author: ZZQ @software: PyCharm @file: leetcode66_加一.py @time: 2018/11/29 16:07 要求:给定一个由整数组成的非空数组所表示 ...

随机推荐

  1. .Net45下HttpClient的几个缺陷

    前言 最近在写WebClientApi这个组件,底层使用HttpClient,发现HttpClient有许多低级的错误,使用者一不小心就可能会正常的去调用它的这些错误,得不到预期的结果.本文我把我认为 ...

  2. spring boot / cloud (十七) 快速搭建注册中心和配置中心

    spring boot / cloud (十七) 快速搭建注册中心和配置中心 本文将使用spring cloud的eureka和config server来搭建. 然后搭建的模式,有很多种,本文主要聊 ...

  3. codeforces 862B B. Mahmoud and Ehab and the bipartiteness

    http://codeforces.com/problemset/problem/862/B 题意: 给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且 ...

  4. java.lang.IllegalArgumentException: node to traverse cannot be null!

    查看HQL的语句是否写错了,是否有在From后面加空格.我就是没有加空格报了错误! return sessionFactory.getCurrentSession().createQuery(&quo ...

  5. mysql死锁+解决

    自己作死,navicat不恰当的操作导致了表死锁,操作如下: 给表新加字段:name 没有选择允许为空,但是有没有设置初始值,所以运行的结果就是数据库表里有了name不允许为空但是确实为空的记录: 然 ...

  6. 从Leetcode的Combination Sum系列谈起回溯法

    在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...

  7. Azure Powershell使用已有Image创建ARM非托管磁盘虚拟机

    生成Image映像文件,记录好Image的URL(下面URL为测试URL,具体请参考实际):ImageURL:https://hlmrgstoragen.blob.core.chinacloudapi ...

  8. IDEA用maven创建springMVC项目和配置

    工具准备:IDEA2016.3 Java jdk 1.8 1.DEA创建项目 新建一个maven project,并且选择webapp原型.  然后点击next  这里的GroupId和Artifac ...

  9. 【个人笔记】《知了堂》express模块

    NPM  包管理器 Node package module  ==>简称npm 类似的bower 安装express 1.全局Npm install express -g 2.项目中安装 项目中 ...

  10. Pycharm中如何加载多个项目?

    今天在使用Pycharm工具练习Python时遇到一个疑问:在已存有项目A工程的前提下如何新建另一个项目B,且两者并存? 基本操作步骤: 在File下拉项中选择"New Project&qu ...