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

The digits are stored such that the most significant digit is at the head of the list.

【思路】

数组从后往前遍历,加一如果不需要进位的话直接返回加一后的结果,需要进位的话继续向前遍历。如果最前面一位需要进位,则生成一个第一位为1,其他位为0,长度加一的新数组。代码如下:

 public class Solution {
public int[] plusOne(int[] digits) {
if(digits==null || digits.length==0) return digits;
int i;
for(i = digits.length - 1; i >= 0; i--){
if(digits[i] + 1 < 10){
digits[i] += 1;
break;
}
else digits[i] = 0;
}
if(i < 0){
int[] newdigits = new int[digits.length+1];
newdigits[0] = 1;
return newdigits;
}
return digits;
}
}

LeetCode OJ 66. Plus One的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  5. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  7. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  8. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

随机推荐

  1. 如何设置linux的文件和目录的权限

    1 字符表示法 1)chmod [-R] 权限(mode)file   注:[-R]会将目录下所有权限都设置成指定的 who operator permission u(owner) +增加权限 r( ...

  2. C# 常用接口学习 IEnumerable<T>

    作者:乌龙哈里 时间:2015-10-24 平台:Window7 64bit,Visual Studio Community 2015 本文参考: MSDN IEnumerable<T> ...

  3. Digi. Certificates: Key pairs usages

    In short, we have some sort of algorithms to gen pair of private and public keys. The public key is ...

  4. KVM下windows虚拟机使用virtio驱动

    KVM下windows虚拟机默认disk使用的是Qemu IDE硬盘,网卡默认是rtl8139网卡.为了使kvm主机在相同的配置下,有更好的效率,可以将网卡和磁盘替换成virtio的驱动. windo ...

  5. OGG中断后,重新同步操作

    模拟一下goldengata中断后,重新同步操作: 1.关掉源端抽取进程 GGSCI (20081122-2105) 15> info all Program Status Group Lag ...

  6. 详细解析Java中抽象类和接口的区别(转)

    转自:http://dev.yesky.com/436/7581936.shtml 在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制.正是由于这两种机 ...

  7. Javaweb 第7天 Servlet课程

    Servlet课程 三日大纲 ● 网络概念,专业术语 ● Tomcat使用,发布网站,使用Myeclispe发布网站(搭建环境) ● 编写Servlet,Servlet生命周期 ● 用户注册,显示所有 ...

  8. 为什么PHP(CLI)同一个错误信息会打印两次?

    第一个信息是display_errors输出的,在fpm环境下输出到浏览器那里,而在CLI环境下会打印到屏幕上. display_errors = On 第二个信息是log_errors输出的. lo ...

  9. List分页 参考

    public <T> List<List<T>> splitList(List<T> list, int pageSize) { int listSiz ...

  10. SSH 两个表全套增删改(运动员住宿管理)

    0.创建如下oracle的命令 create table HOTALINFO ( HOTALID ) not null, HOTALNAME ) not null, HOTALADDRESS ) no ...