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的更多相关文章
随机推荐
- 【转】Senior Data Structure · 浅谈线段树(Segment Tree)
本文章转自洛谷 原作者: _皎月半洒花 一.简介线段树 ps: _此处以询问区间和为例.实际上线段树可以处理很多符合结合律的操作.(比如说加法,a[1]+a[2]+a[3]+a[4]=(a[1]+a[ ...
- jQuery隐藏和显示从上往下的实现方法
jquery 显示隐藏方法实现动画效果 方向 显示 隐藏 左上角到右下角 show() hide() 垂直向下 slideDown() slideUp() 水平与垂直两个方向 toggle() 垂直向 ...
- Django REST framework+Vue 打造生鲜电商项目(笔记七)
十.购物车.订单管理和支付功能 1.添加商品到购物车 (1)trade/serializer.py 这里的serializer不继承ModelSerializer,是因为自己写的Serializer更 ...
- cc 视频的使用
1. 先上传视频 2.复制代码 3.贴在页面上就可以使用了 4.通过id指定播放那个视频
- PHP 对参数签名
对参数进行签名防止参数劫持 加入timestamp, 防止DOS攻击(但这次没有实现这个功能,后续再实现) interface BaseToken { /** * @param params arra ...
- E:nth-of-type(n)
E:nth-of-type(n) 语法: E:nth-of-type(n) { sRules } 说明: 匹配同类型中的第n个同级兄弟元素E.深圳dd马达 要使该属性生效,E元素必须是某个元素的子元素 ...
- 011_GoldWave软件安装及使用
(一)软件安装包: 链接:https://pan.baidu.com/s/15c5veooyA8bAYIAgLFOLjg提取码:jiis 复制这段内容后打开百度网盘手机App,操作更方便哦 (二)降低 ...
- Greenplum 表空间和filespace的用法
转载:https://yq.aliyun.com/articles/190 Greenplum支持表空间,创建表空间时,需要指定filespace.postgres=# \h create table ...
- 将Eclipse,MyEclipse等编辑器的项目管理框颜色改为护眼豆沙绿的方法
转载链接:https://blog.csdn.net/caibaoH/article/details/77005977
- Educational Codeforces Round 60 D. Magic Gems
易得递推式为f[i]=f[i-1]+f[i-M] 最终答案即为f[N]. 由于N很大,用矩阵快速幂求解. code: #include<bits/stdc++.h> using names ...