当前位置:网站首页>[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}));
}
}
边栏推荐
- Understanding of indexes in MySQL
- Pytorch's code for visualizing feature maps after training its own network
- 倒置残差的理解
- Ida dynamic debugging apk
- Fully annotated SSM framework construction
- 配置筛选机
- QStringList 的常规使用
- Learn MySQL from scratch - database and data table operations
- Appium自动化测试基础 — APPium安装(一)
- 正则系列之量词(Quantifiers)
猜你喜欢
随机推荐
[jetcache] how to use jetcache
旅游管理系统
MySQL MHA high availability configuration and failover
Awoo's favorite problem (priority queue)
mixconv代码
隐藏用户的创建和使用
104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
Kubernetes创建Service访问Pod
Explain kubernetes network model in detail
JVM有哪些类加载机制?
RestTemplate 远程调用工具类
Cut noodles C language
Wechat open platform scanning code login [easy to understand]
Talk about what parameters ZABBIX monitors
SAP UI5 应用开发教程之一百零四 - SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
死锁的处理策略—预防死锁、避免死锁、检测和解除死锁
好友新书发布,祝贺(送福利)
功能测试报告的编写
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
Deep learning -- data operation