题目描述:

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. 【BZOJ】【2879】【NOI2012】美食节

    网络流/费用流 跟 BZOJ 1070 修车 几乎是一道题,只是这题“要修的车”(即菜)多了很多……几乎是从$n$变成了$n^2$,所以建图的时候就得动态加点…… 也就是说,当一个厨师已经确定了他的后 ...

  2. AnkhSVN 安装

    为 visual Studio 2013 添加 AnkhSVN 步骤 到 https://ankhsvn.open.collab.net/downloads 下载 2.6x或以上 安装 AnkhSvn ...

  3. [转载]实战Linux下VMware虚拟机根目录空间扩充

    [转载]实战Linux下VMware虚拟机根目录空间扩充 (2011-07-31 21:34:34) 转载▼ 标签: 转载   原文地址:实战Linux下VMware虚拟机根目录空间扩充作者:shar ...

  4. Node.js 4.0.0:灵雀云和 OneAPM 的整合测试

    关于 Node.js 4.0.0 稳定版刚刚推出,备受期待,迫不及待地想用它写点东西:此外,要把 Demo 放到 Internet 上得有一个公网 IP ,看到灵雀云挺不错的而且提供域名解析,简直业界 ...

  5. [转载]Spring Bean Definition Inheritance

    Following is the configuration file Beans.xml where we defined "helloWorld" bean which has ...

  6. [Unity菜鸟] Final IK

    由于本人英文较烂,边翻译用户手册边学习. 用户手册  IK Components Final IK 包含许多强大高速的IK组件 Aim  AimIK solver是一个对CCD算法(cyclic co ...

  7. Java学习笔记之:Java引用数据类型之字符串

    一.简介 字符串广泛应用在Java编程中,在Java中字符串属于对象,Java提供了String类来创建和操作字符串. 创建字符串最简单的方式如下: String greeting = "H ...

  8. 用ISO C++实现自己的信号槽(Qt另类学习)

    qtc++objectsignalclassstring   目录(?)[-] Qt信号与槽 引入元对象系统 建立信号槽链接 信号的激活 槽的调用 全家福 零零散散写在后面 Q_OBJECT Conn ...

  9. CentOS 7.0安装Nvidia驱动

    entOS 7.0 Nvidia显卡安装步骤: 1 在英伟达官网下载相应驱动 搜索出相应的驱动后,不要直接点,而是右健,Save Link as... 否则,会出现下载半天没动静的情况. 存放的路径上 ...

  10. python 批量更换图片格式脚本

    问题:将某文件下的所有jpg的图片更换为png的图片 简单的实现: # -*- coding:utf-8 -*- from os.path import splitext import glob fr ...