当前位置:网站首页>[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}));
}
}
边栏推荐
猜你喜欢

vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载

正则系列之量词(Quantifiers)

Selection of all-optical technology in the park - Part 2

3DE resources have nothing or nothing wrong

互联网的智算架构设计
![[untitled]](/img/60/9a56e8b00c386779be13308515b24f.png)
[untitled]

使用 EMQX Cloud 实现物联网设备一机一密验证

Kubernetes创建Service访问Pod

Single step debugging analysis of rxjs observable of operator

元宇宙可能成为互联网发展的新方向
随机推荐
【目标跟踪】|单目标跟踪指标
性能测试计划怎么编写
Object memory layout
死锁的处理策略—预防死锁、避免死锁、检测和解除死锁
Slope compensation
SAP UI5 应用开发教程之一百零四 - SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
详解Kubernetes网络模型
Metauniverse may become a new direction of Internet development
微信开放平台扫码登录[通俗易懂]
Wechat open platform scanning code login [easy to understand]
There is no signal in HDMI in computer games caused by memory, so it crashes
【JetCache】JetCache的使用方法与步骤
QT uses ffmpeg4 to convert the qimage of ARGB to yuv422p
Understanding of inverted residuals
flink sql 命令行 连接 yarn
Yolov5.5 call local camera
Pytorch nn. functional. Simple understanding and usage of unfold()
Ffmpeg learning notes
Understanding of indexes in MySQL
2020-ViT ICLR