Plus One @LeetCode
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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- HDU 5783 Divide the Sequence
Divide the Sequence Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- bjfu1208 中位数
题目是给你一个数x以及一个长度为n的数列,让你往数列里插入y个数,使数列的中位数正好是x,求y的最小值.(其实这题的中位数跟数学里的中位数有一点区别,略去不提) 那么就排完序以后分情况讨论一下就好了. ...
- 从零开始完整Electron桌面开发(1)搭建开发环境
[OTC] # 需要知识 1. 简单的html.javascript.css知识,就是web前端入门知识. 2. 简单命令行的应用,不会也没关系,照着代码敲就行. 3. 下载安装就不说了吧. 4. 本 ...
- 过度拟合(overfitting)
我们之前解决过一个理论问题:机器学习能不能起作用?现在来解决另一个理论问题:过度拟合. 正如之前我们看到的,很多时候我们必须进行nonlinear transform.但是我们又无法确定Q的值.Q过小 ...
- Binder机制
在Android系统的Binder机制中,由一系统组件组成,分别是Client.Server.Service Manager和Binder驱动程序,其中Client.Server和Service Ma ...
- HDFS体系结构
HDFS的设计目标 检测以及快速恢复硬件问题. 流式的数据访问. 移动计算比移动数据的代价小. 简化一致性模型. 超大规模数据集 异构软硬件平台之间的可移植性. HDFS的结构模型HDFS是一个主从的 ...
- delphi 单例模式实现
unit Unit2; interface uses System.SysUtils; type { TSingle } TSingle = class(TObject) private FStr: ...
- Hadoop port to Jxta P2P Framework
https://www.java.net/forum/topic/jxta/jxta-community-forum/hadoop-port-jxta-p2p-framework —————————— ...
- HDU 5813 Elegant Construction (贪心)
Elegant Construction 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...
- POJ 2185 Milking Grid(KMP)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4738 Accepted: 1978 Desc ...