当前位置:网站首页>[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}));
}
}
边栏推荐
猜你喜欢
随机推荐
Yolov5.5 call local camera
Appium automated testing foundation - Supplement: introduction to desired capabilities parameters
【JetCache】JetCache的使用方法与步骤
Using securecrtportable to remotely connect virtual machines
nn.Parameter】Pytorch特征融合自适应权重设置(可学习权重使用)
使用 EMQX Cloud 实现物联网设备一机一密验证
Mysql——》MyISAM存储引擎的索引
MySQL数据库详细学习教程
固定资产管理子系统报表分为什么大类,包括哪些科目
LC501. Mode in binary search tree
Measurement of reference loop gain and phase margin
Favorite transaction code management tool in SAP GUI
MySQL的视图练习题
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
数字货币:影响深远的创新
激发新动能 多地发力数字经济
微信开放平台扫码登录[通俗易懂]
内存导致的电脑游戏中显示hdmi无信号 从而死机的情况
Operation category read is not supported in state standby
Understanding of inverted residuals