import java.util.Arrays;

/**
* Plus One
*
* Given a number represented as an array of digits, plus one to the number.
*/
public class S66 { public static void main(String[] args) {
int[] digits = {9,9,9};
// int[] digits = {0};
System.out.println(Arrays.toString(plusOne(digits)));
} public static int[] plusOne(int[] digits) {
int i = digits.length-1;
int overflow = 0; // 用来表示是否overflow了
// 从尾到头加
while(i >= 0){
if(digits[i]+1 > 9){ // 加完大于9的情况
digits[i] = 0;
overflow = 1;
i--;
}else{ // 加完小于10的情况
digits[i] = digits[i]+1;
return digits;
}
} // 这种情况是当前位数不够用,就必须新开数组,
// 处理首位
if(overflow > 0){
int[] newDigits = new int[digits.length+1];
System.arraycopy(digits, 0, newDigits, 1, digits.length);
newDigits[0] = 1;
newDigits[1] = 0;
return newDigits;
} return digits;
} }

Plus One @LeetCode的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. ch02-HTML的基本概念

    HTML的基本概念 Ch02: HTML的基本概念 1, 标记: 每一组HTML标记,都会被放在"<>"里面,用于控制里面的文字显示效果及其它一些用途. 语法: 开始标 ...

  2. 修改eOS wingpanel的透明度与颜色

    打开终端,输入: sudo scratch-text-editor /usr/share/themes/elementary/gtk-3.0/apps.css 修改.panel与.panel-shad ...

  3. asp.net MVC 应用程序的生命周期(下)

    看看上面的UrlRoutingModule源码里面是怎么实现Init方法的,Init()方法里面我标注红色的地方: application.PostResolveRequestCache += new ...

  4. (转载) 数组a[]={3,5,2,4,1,8},要求从a中找出所有“和”等于10的子集

    背包问题.     不过就这道题目本身而言,由于集合a中只要6个元素,而不是成千上万,所以可以使用更直观的办法:     只要你能通过程序给出数组a中元素所组成的集合的所有的子集合(幂集),那么只需在 ...

  5. Android中的音频播放(MediaPlayer和SoundPool)

    Android中音频和视频的播放我们最先想到的就是MediaPlayer类了,该类提供了播放.暂停.停止.和重复播放等方法.该类位于android.media包下,详见API文档.其实除了这个类还有一 ...

  6. Matlab编程实例(4) 相位角与相关系数曲线

    %相位角与相关系数曲线 close all; clear all; Samp1=200;  %设置信号的采样精度 Samp2=200;  %设置相位角p分割精度 A=10;%信号幅值 w=1;%信号角 ...

  7. mysql DDL语句

    sql语言分为三个级别. 1.ddl 语句 ,数据定义语句,定义了数据库.表.索引等对象的定义.常用语句包含:create.drop.alter. 2.dml 语句 ,数据操纵语句,用于添加.删除.更 ...

  8. 《Python 学习手册4th》 第十七章 作用域

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...

  9. oracleasm方式创建ASM

    1.准备oracleasm包 [root@localhost oracle]# uname -r 2.6.18-164.el5 [oracle@localhost ~]$ ls -l total 26 ...

  10. Google C++ 编程规范总结

    一.头文件 #define 的保护 项目 foo 中的头文件 foo/src/bar/baz.h 按如下方式保护: #ifndef FOO_BAR_BAZ_H_ #define FOO_BAR_BAZ ...