问题描述:一个数组每一位代表一个数字的每一位。数字高位在数组的低位。求数字加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. IntelliJ中的main函数和System.out.println()快捷输入方式

    转自:https://blog.csdn.net/assassinsshadow/article/details/73557375 main快捷输入 psvm System.out.println() ...

  2. 讨论cocos2d-x字体绘制原理和应用方案

    转自:http://blog.csdn.net/langresser_king/article/details/9012789 个人一直认为,文字绘制是cocos2d-x最薄弱的环节.对于愤怒的小鸟之 ...

  3. 手动爬虫之报头及代理封装类(python3)

    本人刚刚学习爬虫,见每次都需要添加报头比较繁琐,故将该过程封装为Url_ProxyHelper类,代码如下 import urllib.request as ur class Url_ProxyHel ...

  4. 史上最易懂的大数据 OTO

    史上最易懂的大数据 OTO http://network.51cto.com/art/201503/467068.htm 终于有人把O2O.C2C.B2B.B2C的区别讲透了 http://tech. ...

  5. [刷题]ACM ICPC 2016北京赛站网络赛 D - Pick Your Players

    Description You are the manager of a small soccer team. After seeing the shameless behavior of your ...

  6. 2 CDuiString的bug

    重温了一下 Effective C++,发现这就是条款24所指出的问题,看来读书百遍不如写代码一遍啊 在Notify处理消息时会有很多if语句,我通常喜欢把常量放在双等号前面,变量放在后面,比如:   ...

  7. php 正则表达式二.基本语法

    官方手册正则语法:http://php.net/manual/zh/reference.pcre.pattern.syntax.php 正则表达式在线测试工具:regexpal 正则表达式的匹配先后顺 ...

  8. Extjs3 combobox使用

    Combobox 在程序中应用十分普遍,每个combobox的选项 一般对应两个值:一个用于前台显示的值,一个与显示值对应的value值.在后台获取value的值需使用combobox的 Hidden ...

  9. 接口测试工具 — jmeter(数据库操作)

    1.导入jdbc jar包 2.配置MySQL连接 3.执行sql语句

  10. [luogu4556]雨天的尾巴

    [luogu4556]雨天的尾巴 luogu 发现是一顿子修改然后再询问,那么把修改树上差分一下再线段树合并 但是... 如果你只有35分... https://www.luogu.org/discu ...