LeetCode_66. Plus One
66. Plus One
Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
package leetcode.easy;
public class PlusOne {
@org.junit.Test
public void test() {
int[] digits11 = { 1, 2, 3 };
int[] digits21 = { 4, 3, 2, 1 };
int[] digits12 = plusOne(digits11);
int[] digits22 = plusOne(digits21);
for (int i = 0; i < digits12.length; i++) {
System.out.print(digits12[i]);
}
System.out.println();
for (int i = 0; i < digits22.length; i++) {
System.out.print(digits22[i]);
}
System.out.println();
}
public int[] plusOne(int[] digits) {
for (int i = digits.length - 1; i >= 0; i--) {
if (digits[i] != 9) {
digits[i]++;
break;
} else {
digits[i] = 0;
}
}
if (digits[0] != 0) {
return digits;
} else {
int[] result = new int[digits.length + 1];
result[0] = 1;
return result;
}
}
}
LeetCode_66. Plus One的更多相关文章
随机推荐
- linux下环境管理anaconda3
我之前在centos之安装单独python3.6,大家都知道centos自带python2.7,通过输入python,和python3来控制想要使用python2,或者python3,如今想要要在li ...
- 并发编程大师系列之:wait/notify/notifyAll/condition
1. wait().notify()和notifyAll()方法是本地方法,并且为final方法,无法被重写. 2. 调用某个对象的wait()方法能让当前线程阻塞,并且当前线程必须拥有此对象的mon ...
- [GraphQL] Query Lists of Multiple Types using a Union in GraphQL
Unions are used when we want a GraphQL field or list to handle multiple types of data. With a Union ...
- 洛谷 P2615 神奇的幻方 题解
每日一题系列day1 打卡 Analysis 水货模拟,不多说了 #include<iostream> #include<cstdio> #include<cstring ...
- Docker 学习10 Docker的系统资源限制及验证
一.资源限制 二.内存限制 1.OOME 每一个进程都会有oom_adj(oom计算分数的权重)值,此值越大,oom_score(oom得分)越高,越容易被干掉,因此非常非常重要的容器化应用,一开始就 ...
- promise 及 setTimeout 执行顺序
setTimeout(function() { console.log(1); }, 0); new Promise(function(res, rej) { res(2); console.log( ...
- rdma centos 7.3安装
rdma centos 7.3安装 corasql0人评论7680人阅读2017-05-28 16:29:40 1.安装依赖包 yum install epel-release -y yum ...
- docker安装mysql5.7 数据挂载
docker安装mysql5.7,并数据卷挂载到主机 # docker 中下载 mysql docker pull mysql:5.7 #启动 docker run --name mysql3306 ...
- docker 搭建registry
Docke官方提供了Docker Hub网站来作为一个公开的集中仓库.然而,本地访问Docker Hub速度往往很慢,并且很多时候我们需要一个本地的私有仓库只供网内使用.Docker仓库实际上提供两方 ...
- BZOJ2716天使玩偶
不会KD-tree怎么办?CQD硬搞. 建立正常的平面直角坐标系,首先我们只考虑在目标点左下角的点对目标点的贡献,由于左下点的横纵坐标都小于目标点,那么曼哈顿距离就可以化简了,绝对值去掉后,得到$x2 ...