当前位置:网站首页>【日常训练】66. 加一
【日常训练】66. 加一
2022-07-01 21:47:00 【Puppet__】
题目
给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。
最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。
你可以假设除了整数 0 之外,这个整数不会以零开头。
示例 1:
输入:digits = [1,2,3]
输出:[1,2,4]
解释:输入数组表示数字 123。
示例 2:
输入:digits = [4,3,2,1]
输出:[4,3,2,2]
解释:输入数组表示数字 4321。
示例 3:
输入:digits = [0]
输出:[1]
提示:
1 <= digits.length <= 100
0 <= digits[i] <= 9
代码
package dayLeetCode;
public class dayleetcode66 {
/** * 关键是处理9 * (1)最后一位不是9的话,直接+1 * (2)最后几位都是9的话,找到从后往前第一个非9的元素,将其+1,并将其后的9全部置0 * (3)全部是9的话,扩位,最前面加个1 * @param digits * @return */
public int[] plusOne(int[] digits) {
int n = digits.length;
for (int i = n -1; i >= 0 ; i--) {
if (digits[i] != 9){
digits[i]++;
for (int j = i + 1; j < n; j++){
digits[j] = 0;
}
return digits;
}
}
digits = new int[n + 1];
digits[0] = 1;
return digits;
}
public static void main(String[] args) {
dayleetcode66 obj = new dayleetcode66();
System.out.println(obj.plusOne(new int[]{
1, 2, 3}));
}
}
边栏推荐
- Several ways of writing main function in C
- Communication between browser tab pages
- 比较版本号[双指针截取自己想要的字串]
- Count the number of each character in the character
- MySQL数据库详细学习教程
- CIO's discussion and Analysis on the definition of high-performance it team
- One of the basic learning of function
- AirServer2022最新版功能介绍及下载
- QT uses ffmpeg4 to convert the qimage of ARGB to yuv422p
- Mysql——》MyISAM存储引擎的索引
猜你喜欢

互联网的智算架构设计

C#/VB.NET 给PDF文档添加文本/图像水印

MySQL系列之事务日志Redo log学习笔记

Go - exe corresponding to related dependency

Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue

I received a letter from CTO inviting me to interview machine learning engineer

One of the basic learning of function

详解JMM

Why does blocprovider feel similar to provider?
![[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial](/img/ac/655fd534ef7ab9d991d8fe1c884853.png)
[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
随机推荐
MySQL的存储过程
Mysql——》Innodb存储引擎的索引
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
企业架构与项目管理的关联和区别
业务可视化-让你的流程图'Run'起来
The correct way to set the bypass route
Classify boost libraries by function
Design and practice of new generation cloud native database
Clean up system cache and free memory under Linux
LC501. 二叉搜索树中的众数
互联网的智算架构设计
【生态伙伴】鲲鹏系统工程师培训
Use of vscode
3DE 资源没东西或不对
Interview question: what is the difference between MySQL's Union all and union, and how many join methods MySQL has (Alibaba interview question) [easy to understand]
Recent public ancestor offline practice (tarjan)
记录一次spark on yarn 任务报错 Operation category READ is not supported in state standby
LIS (longest ascending subsequence) problem that can be understood [easy to understand]
Getting started with the lockust series
linux下清理系统缓存并释放内存