当前位置:网站首页>[daily training] 66 add one-tenth
[daily training] 66 add one-tenth
2022-07-01 22:49:00 【Puppet__】
subject
Given a by Integers Composed of Non empty The nonnegative integer represented by an array , Add one to the number .
The highest digit is placed at the top of the array , Each element in the array stores only a single number .
You can assume that except for integers 0 outside , This integer will not start with zero .
Example 1:
Input :digits = [1,2,3]
Output :[1,2,4]
explain : Input array represents number 123.
Example 2:
Input :digits = [4,3,2,1]
Output :[4,3,2,2]
explain : Input array represents number 4321.
Example 3:
Input :digits = [0]
Output :[1]
Tips :
1 <= digits.length <= 100
0 <= digits[i] <= 9
Code
package dayLeetCode;
public class dayleetcode66 {
/** * The key is to deal with 9 * (1) The last one is not 9 Words , direct +1 * (2) The last few are 9 Words , Find the first non from back to front 9 The elements of , Put it +1, And then 9 Set all 0 * (3) All is 9 Words , Expand , Add a... At the top 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}));
}
}
边栏推荐
猜你喜欢

Mysql5.7 set password policy (etc. three-level password transformation)

【图像分割】2021-SegFormer NeurIPS

园区全光技术选型-中篇

MySQL5.7 设置密码策略(等保三级密码改造)

Single step debugging analysis of rxjs observable of operator

分享一个一年经历两次裁员的程序员的一些感触

H5 model trained by keras to tflite

Understanding of indexes in MySQL

Pytorch sharpening chapter | argmax and argmin functions

“信任机器”为发展赋能
随机推荐
Single step debugging analysis of rxjs observable of operator
功能测试报告的编写
【目标跟踪】|单目标跟踪指标
map容器
redis配置文件中常用配置详解[通俗易懂]
下班前几分钟,我弄清了v-model与.sync的区别
internal field separator
【JetCache】JetCache的使用方法与步骤
Origin2018 installation tutorial "recommended collection"
YOLOv5.5 调用本地摄像头
Redis configuration and optimization
C#/VB. Net to add text / image watermarks to PDF documents
Resttemplate remote call tool class
nn.Parameter】Pytorch特征融合自适应权重设置(可学习权重使用)
Ida dynamic debugging apk
spark analyze命令使用及其作用 map join broadcast join 广播join
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 release | geek headlines
友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…
C#/VB.NET 给PDF文档添加文本/图像水印
2020-ViT ICLR