问题描述:一个数组每一位代表一个数字的每一位。数字高位在数组的低位。求数字加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. 170120、java 如何在pdf中生成表格

    1.目标 在pdf中生成一个可变表头的表格,并向其中填充数据.通过泛型动态的生成表头,通过反射动态获取实体类(我这里是User)的get方法动态获得数据,从而达到动态生成表格. 每天生成一个文件夹存储 ...

  2. Vue.js_判断与循环

    一.判断,条件语句 1.一元表达式判断 {{ ok ? 'show' : 'hide' }} 2.if判断 v-if='ok' <ol id="ifGrammar"> ...

  3. 如何看懂ORACLE执行计划

    如何看懂Oracle执行计划 一.什么是执行计划 An explain plan is a representation of the access path that is taken when a ...

  4. 用户登陆状态,ios开发用户登陆

    IOS开发之记录用户登陆状态,ios开发用户登陆 上一篇博客中提到了用CoreData来进行数据的持久 化,CoreData的配置和使用步骤还是挺复杂的.但熟悉CoreData的使用流程后,CoreD ...

  5. Eclipse 介绍

    设置背景的插件: Darkest Dark Theme 添加 properties 插件: Properties Editor Git 插件: Egit 常用快捷键 command + 1 : 代码提 ...

  6. echarts系列之动态修改柱状图颜色

    echarts根据某一变量动态修改柱状图颜色 1.option中参数配置项series { "name":"Android", "type" ...

  7. MySQL给字段唯一索引的三种方法

    建表时添加 DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `stu_id` ) NOT NULL AUTO_INCREMENT, ` ...

  8. 4 TensorFlow入门之dropout解决overfitting问题

    ------------------------------------ 写在开头:此文参照莫烦python教程(墙裂推荐!!!) ---------------------------------- ...

  9. beego——ORM使用方法

    先来看一个简单示例: models.gp package main import ( "github.com/astaxie/beego/orm" ) type User stru ...

  10. go——类型的本质

    在声明一个新类型之后,声明一个该类型的方法之前,需要先回答一个问题:这个类型的本质是什么. 如果给这个类型增加或删除某个值,是要创建一个新值,还是要更改当前的值? 如果是要创建一个新值,该类型的方法就 ...