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的更多相关文章
随机推荐
- .NET Core、EF、Dapper、MySQL 多种方式实现数据库操作(动态注册实体类)
目录 前言 一.技术选型 二.遇到的坑 2.1..NET Core 下 EF 的问题 2.2.数据库实体类的注册 切记坑 前言 最近在学习.研究 .NET Core 方面的知识,动手搭建了一些小的 D ...
- [Dart] Manipulate Lists/Arrays in Dart
We will learn how to work with Lists using a variety of methods made available in the dart:core libr ...
- Codeforces Round #603 (Div. 2) A,B,C,D【E题待补】
#include<bits/stdc++.h> using namespace std; #define int long long signed main(){ int _; cin&g ...
- 007——转载-MATLAB读取文件夹下的文件名
(一)参考文献:https://blog.csdn.net/liutaojia/article/details/84899923 (二)第一步:获取文件夹下某类型数据的所有文件名 主要包括三个步骤: ...
- 遇到一张jpg的图片打不开,ps打不开,fireworks,打不开,ie8浏览器上显示不了,其他的浏览器没问题
1.在photoshop上报错; 2.在fireworks上报错 3.ie8上 其他的图片都可以,就这张不可以,没发现什么不同的地方,都是jpg格式的呀,而且谷歌浏览器能显示出来; 处理方法: 1.选 ...
- C++标准库分析总结(二)——<模板,分配器,List>
本节主要总结模板及其类模板分类以及STL里面的分配器.容器内部结构以及容器之间的关系和分类,还介绍了容器中List的结构分布 1.源代码版本介绍 1.1 VC的编译器源码目录: 2.类模板 2.1 类 ...
- zabbix(12)使用Grafana
一.Grafana介绍 Grafana是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知.它主要有以下六大特点: 1.展示方式:快速灵活的客户端图表,面板插 ...
- Tcl循环语句
for 开始 判断语句 变量自增(自检) 循环体 示例代码: for {set i 0} {$i<10} {incr i} { puts "I is: $i " } 运行结果 ...
- Windows下的apache maven安装与配置
去到官网http://maven.apache.org/download.cgi下载压缩包我选择的是二进制zip压缩文件. 解压并配置压缩文件的目录到MAVEN_HOME环境变量,添加解压文件下的bi ...
- VSCode前端文件(html文件)如何以服务器模式打开?
方法1: VSCode前端文件(html文件)如何以服务器模式打开?比如工程下有一个A.html文件,想在VSCode里面直接操作,就想Webstorm一样,以http://localhost/xxx ...