题目描述:

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

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

解题思路:

设置进位,如果数组不够的话记得重新开辟空间。

代码如下:

public class Solution {
public int[] plusOne(int[] digits) {
int carry = 0;
if(digits == null || digits.length == 0)
return null;
else
digits[digits.length - 1] += 1;
for(int i = digits.length - 1; i >= 0; i--){
int cur = carry + digits[i];
carry = cur / 10;
digits[i] = cur % 10;
}
if(carry != 0){
int[] newDigits = new int[digits.length + 1];
System.arraycopy(digits, 0, newDigits, 1, digits.length);
newDigits[0] = carry;
return newDigits;
} else {
return digits;
}
}
}

  

Java [Leetcode 66]Plus One的更多相关文章

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

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

  2. Java实现 LeetCode 66 加一

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

  3. [LeetCode]66.加一(Java)

    原题地址: plus-one 题目描述: 给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 ...

  4. Java [leetcode 1] Two Sum

    小二终于开通博客了,真是高兴.最近在看Java,所以就拿leetcode练练手了.以后我会将自己解体过程中的思路写下来,分享给大家,其中有我自己原创的部分,也有参考别人的代码加入自己理解的部分,希望大 ...

  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—66、88、118、119、121 Array(Easy)

    66. Plus One Given a non-negative integer represented as a non-empty array of digits, plus one to th ...

  7. 2017-3-7 leetcode 66 119 121

    今天纠结了一整天============================================================== leetcode66 https://leetcode.c ...

  8. [LeetCode] 66. Plus One 加一

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

  9. leetcode 66

    66. Plus One Given a non-negative number represented as an array of digits, plus one to the number. ...

随机推荐

  1. Unit Test Generator

           

  2. JavaScript高级---适配器模式

    一.设计模式 javascript里面给我们提供了很多种设计模式: 工厂.桥.组合.门面.适配器.装饰者.享元.代理.观察者.命令.责任链 在前面我们实现了工厂模式和桥模式 工厂模式 : 核心:为了生 ...

  3. node-firefox 二三事

    编者按:本文作者为 Soledad Penadés, Sole 在 Mozilla 的 Tech Evangelism 团队工作,帮助人们在网络上创造神奇的东西.本文主要介绍node-firefox的 ...

  4. Java Memory Basic

    转自: http://www.blogjava.net/justinchen/archive/2009/justinchen/archive/2009/01/08/248738.html GC and ...

  5. POJ 1795

    DNA Laboratory Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 1425   Accepted: 280 Des ...

  6. Sublime Text 3配置与vim模式(待完整)

    Sublime Text 3通过设置默认值与用户值的方式,来进行配置.默认值不允许更改,用户值是用户进行配置.同一属性,当用户值存在时,默认值就无效.打开Preference,如图: 先贴下我的Set ...

  7. C#实现身份证号码验证的方法

    本文实例讲述了C#实现身份证号码验证的方法.分享给大家供大家参考.具体实现方法如下: 随着现在互联网的发展,越来越多的注册用户的地方都用到了身份证,那么对于输入的身份证如何验证呢?看下面的代码,其实很 ...

  8. lintcode:数飞机

    数飞机 给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机? 如果多架飞机降落和起飞在同一时刻,我们认为降落有优先权. 样例 对于每架飞机的起降时间列表 ...

  9. GC垃圾回收之GC.KeepAlive方法

    http://msdn.microsoft.com/zh-cn/library/system.gc.keepalive.aspx http://www.cnblogs.com/ren700622/ar ...

  10. ios UIView常用动画效果

    一 //调用 1 2 3 4 5 6 if(m_viewScenario.superview == nil)<br>{     m_viewScenario.alpha = 1.0;    ...