问题描述:一个数组每一位代表一个数字的每一位。数字高位在数组的低位。求数字加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. coreldraw X6 cdrX6下载激活工具

    coreldraw X6 cdrX6下载激活工具 百度网盘 CDRX6下载 激活教程什么的请参考 低吟浅唱 博客

  2. 品友推广的投放原理 RTB:Real Time Bidding(实时竞价) DSP:Demand-Side Platform(需求方平台) 广告交易平台:AD Exchange

    总结: 1.实时竞价 0.1秒出价各个广告主出价,投放价高者: RTB(Real Time Bidding)实时竞价,是一种利用第三方技术在数以百万计的网站或移动端针对每一个用户展示行为进行评估以及出 ...

  3. 分布式计算 要不要把写日志独立成一个Server Remote Procedure Call Protocol

    w https://en.wikipedia.org/wiki/Remote_procedure_call In distributed computing a remote procedure ca ...

  4. You must reset your password using ALTER USER

    mac mysql error You must reset your password using ALTER USER statement before executing this statem ...

  5. logging/re - 总结

    logging 模块 很多程序都有记录日志的需求 logging的日志可以分为 debug(), info(), warning(), error() and critical()5个级别 1.输出到 ...

  6. Java 之Object 类

    Object 类: 所有类的根类, 是不断向上抽取而来, 具备着所有对象都具备的共性内容. 常用共性方法 boolean equals(Object obj) : 判断两个对象是否相等. 默认比较的是 ...

  7. travelsal all files in a dir using recursion shell

    #!/bin/bash function getdir(){ ` do dir_or_file=$"/"$element if [ -d $dir_or_file ] then g ...

  8. django一些配置与ORM

  9. 科班学习java遇到瓶颈,每天云里雾里怎么办?

    声明:这个问题困扰了我好久,今天在知乎找到了答案.知乎链接https://www.zhihu.com/question/24240982,感谢大神@Tony He的回答. 作者:Tony He链接:h ...

  10. xlrd、xlwt操作execl表格

    在python中操作execl进行数据读写的时候,可以使用xlrd进行文件的读取,使用xlwt将数据写入execl中. 1.xlrd xlwt用来读取execl中的数据,常见的用法如下. (1)打开e ...