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的更多相关文章
随机推荐
- vue 用户登录 路由拦截 vuex cookie
功能: // 页面跳转后发送ajax请求给后端 请求详细信息 //点击课程推荐跳转到推荐课程详细 // 页面刚加载立即执行函数 = mounted <template> <div&g ...
- HDU-1465-不容易系列之一(容斥)
链接: https://vjudge.net/problem/HDU-1465 题意: 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好"一件"事情尚且不易 ...
- Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes
链接: https://codeforces.com/contest/1251/problem/B 题意: A palindrome is a string t which reads the sam ...
- kalilinux MSF数据库的连接
需要自动连接数据库.如下设置.
- 采用非常规方法(非gprecoverseg) 恢复greenplum数据库
greenplum数据库中mirror的作用就是作为primary的备份存在.那么恢复down掉的mirror或primary时,是否可以直接复制文件从primary或mirror到对应的mirror ...
- GreenPlum 锁表以及解除锁定
最近遇到truncate表,无法清理的情况,在master节点查看加锁情况,并未加锁这种情况极有可能是segment节点相关表加了锁,所以遇到这种情况除了排查master节点的锁,所有的segment ...
- Bzoj 1566: [NOI2009]管道取珠(DP)
1566: [NOI2009]管道取珠 Time Limit: 20 Sec Memory Limit: 650 MB Submit: 1558 Solved: 890 [Submit][Status ...
- P2637 第一次,第二次,成交!
题目描述 因为奶牛们的节食运动(奶牛还节食?)给农夫JOHN余下了一大批干草无法处理,所以他准备要开一个拍卖会去出售他的干草.他有N(1<=N<=1000)批干草(每批大约100捆).他的 ...
- 使用Ajax和一般处理程序实现文件上传与下载
1.使用HTML的input标签 <input type="file" multiple="multiple" id="file_load&qu ...
- centos7haproxy+keepalive
1部署keepalived 1.1下载keepalived源码包,并解压# wget http://www.keepalived.org/software/keepalived-1.4.2.tar.g ...