问题描述:一个数组每一位代表一个数字的每一位。数字高位在数组的低位。求数字加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. resolution will not be reattempted until the update interval of vas has elap

    转自:http://kia126.iteye.com/blog/1785120 maven在执行过程中抛错: 引用 ... was cached in the local repository, re ...

  2. Spring容器初始化的时候如何添加一个定时器?

    昨天遇到这个问题,在项目启动的时候添加一个定时器隔一段时间扫描有没有定时发送的邮件(当然也可以是你自己的业务逻辑),也在网上找了资料,加上自己的修改,终于成功了.所以来做个记录. 1.ServletC ...

  3. Subscription

  4. apktool 工具

      下载 https://code.google.com/p/android-apktool/ apktool_2.0.0rc2.jar 和apktool linux脚本 ln –s apktool_ ...

  5. poj 1182 食物链 (带关系的并查集)

      食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动 ...

  6. “CMake”这个名字是“cross platform make”

    cmake_百度百科 https://baike.baidu.com/item/cmake/7138032?fr=aladdin CMake 可以编译源代码.制作程序库.产生适配器(wrapper). ...

  7. The Log: What every software engineer should know about real-time data's unifying abstraction

    http://engineering.linkedin.com/distributed-systems/log-what-every-software-engineer-should-know-abo ...

  8. 聊聊 Java 中日期的几种常见操作 —— 取值、转换、加减、比较

    Java 的开发过程中免不了与 Date 类型纠缠,准备总结一下项目经常使用的日期相关操作,JDK 版本 1.7,如果能够帮助大家节约那么几分钟起身活动一下,去泡杯咖啡,便是极好的,嘿嘿.当然,我只提 ...

  9. Hurst指数以及MF-DFA

    转:https://uqer.io/home/ https://uqer.io/community/share/564c3bc2f9f06c4446b48393 写在前面 9月的时候说想把arch包加 ...

  10. 推荐系统第2周--itemCF和userCF

    推荐系统分类 基于应用领域分类:电子商务推荐,社交好友推荐,搜索引擎推荐,信息内容推荐基于设计思想:基于协同过滤的推荐,基于内容的推荐,基于知识的推荐,混合推荐基于使用何种数据:基于用户行为数据的推荐 ...