当前位置:网站首页>[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}));
}
}
边栏推荐
- Yyds dry goods inventory # solve the real problem of famous enterprises: egg twisting machine
- 104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
- 旅游管理系统
- Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
- 利用SecureCRTPortable远程连接虚拟机
- 好友新书发布,祝贺(送福利)
- YOLOv5.5 调用本地摄像头
- Understanding of indexes in MySQL
- ECMAScript 2022 正式发布,有你了解过的吗?
- 447 Bili Bili noodles warp 1
猜你喜欢
随机推荐
Cloud Vulnerability Global Database
LC501. 二叉搜索树中的众数
Awoo's favorite problem (priority queue)
redis配置文件中常用配置详解[通俗易懂]
Yolov5.5 call local camera
[literacy] deep / shallow, local / global features in machine learning image processing
ECMAScript 2022 正式发布,有你了解过的吗?
Redis configuration and optimization
Share some feelings of a programmer who has experienced layoffs twice a year
Selection of all-optical technology in the park - Part 2
Sogou wechat app reverse (II) so layer
Pytorch's code for visualizing feature maps after training its own network
el-input文本域字数限制,超过显示变红并禁止输入
Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
Pytorch nn.functional.unfold()的简单理解与用法
SAP 智能机器人流程自动化(iRPA)解决方案分享
cvpr2022 human pose estiamtion
基准环路增益与相位裕度的测量
pytorch训练自己网络后可视化特征图谱的代码
SAP ui5 application development tutorial 104 - multi select support for SAP ui5 table controls and how to use code to select multiple table row items at a time









