问题描述:一个数组每一位代表一个数字的每一位。数字高位在数组的低位。求数字加1后得到新数组。

算法分析:要从数组的高位到低位进行遍历。

public class PlusOne
{
public int[] plusOne(int[] digits)
{
int len = digits.length;
int carry = 0;
digits[len-1] += 1;
if(digits[len-1] >= 10)
{
digits[len-1] = digits[len-1]-10;
carry = 1;
}
for(int i = len-2; i >=0 ; i --)
{
digits[i] += carry;
if(digits[i] >= 10)
{
digits[i] = digits[i] - 10;
carry = 1;
}
else
{
carry = 0;
}
}
if(carry == 1)
{
int[] array = new int[len+1];
for(int i = len; i > 0; i --)
{
array[i] = digits[i-1];
}
array[0] = 1;
return array;
}
else
{
return digits;
}
} public int[] plusOne2(int[] digits)
{
int n = digits.length;
for(int i = n-1; i >=0; i --)
{
if(digits[i] < 9)
{
digits[i]++;
return digits;
}
digits[i] = 0;
}
int[] array = new int[n+1];
array[0] = 1;
return array;
}
}

PlusOne的更多相关文章

  1. leetcode — plus-one

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...

  2. LeetCode - PlusOne

    题意:给一个数按位存放在一个int数组中,要求返回这个数加一后的数组. 懒人解法: public class Solution { public int[] plusOne(int[] digits) ...

  3. [LeetCode 题解]: plusOne

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a no ...

  4. [LeetCode] Plus One Linked List 链表加一运算

    Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...

  5. [LeetCode] Plus One 加一运算

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

  6. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  7. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  8. 全部leetcode题目解答(不含带锁)

    (记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.)   88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...

  9. LeetCode-66-Plus One

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

随机推荐

  1. vbs 修改Administrator帐号密码

    Dim WshShell, oExec Set wshShell = CreateObject("WScript.Shell") Set objFSO = CreateObject ...

  2. java.lang.IllegalStateException: Queue full

    其实异常说的很清楚 队列满了! ArrayBlockingQueue FIFO 的队列: ArrayBlockingQueue内部是通过一个Object数组和一个ReentrantLock实现的.同时 ...

  3. Tomcat Server

    Tomcat Server的组成部分: 站在框架的顶层的是Server和ServiceServer:servletcontainer Service:Service是这样一个集合:它由一个或者多个Co ...

  4. 部署Node.js的应用

    原创:作者 mashihua 最近Node.js很火,让很多的前端看到了可以直接从前端写到后端的希望.但是每次部署一个Node.js的应用却让前端苦恼不已.每次登陆服务器,用自己不熟悉的方式从版本控制 ...

  5. BeanUtils.copyProperties()

    BeanUtils.copyProperties() PropertyUtils.copyProperties() 通过反射将一个对象的值赋值个另外一个对象(前提是对象中属性的名字相同). 后付前 P ...

  6. django 中模板语言的各种用法

    模板 1.视图中使用模板 模版的创建过程,对于模版,其实就是读取模版(其中嵌套着模版标签),然后将 Model 中获取的数据插入到模版中,最后将信息返回给用户 1.普通方法:HTML被直接硬编码在 P ...

  7. django的安装、文件解释与基本命令

    1.安装 pip install django==1.9.8 2.新建一个django project django-admin startproject mysite #创建工程文件 cd mysi ...

  8. 编译hadoop2.6.0 cdh 5.4.5 集成snappy压缩

    原文地址:http://www.cnblogs.com/qiaoyihang/p/6995146.html 自带的为32位库,故需要把64为重编译进去 1.下载源码:http://archive-pr ...

  9. Appium 输入中文

    文章出处 http://www.cnblogs.com/ljfight/p/6089163.html 在做app自动化过程中会踩很多坑,咱们都是用中文的app,所以首先要解决中文输入的问题!本篇通过屏 ...

  10. Matplot相关(一)

    ——————————缩写定义—————————— import matplotlib.pyplot as plt import matplotlib as mpl ——————————函数解析———— ...