当前位置:网站首页>[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}));
}
}
边栏推荐
- FFMpeg学习笔记
- Slope compensation
- 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
- 分享一个一年经历两次裁员的程序员的一些感触
- Wechat open platform scanning code login [easy to understand]
- Rust language - Introduction to Xiaobai 05
- MySQL中对于事务的理解
- Learn MySQL from scratch - database and data table operations
- 配置筛选机
- SAP GUI 里的收藏夹事务码管理工具
猜你喜欢
随机推荐
C#/VB.NET 给PDF文档添加文本/图像水印
2020-ViT ICLR
阿洛迷茫后的思考
447-哔哩哔哩面经1
LC501. Mode in binary search tree
104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
flink sql-client 使用 对照并熟悉官方文档
[jetcache] how to use jetcache
微信开放平台扫码登录[通俗易懂]
Niuke monthly race - logarithmic sum in groups
Deep learning -- data operation
MySQL数据库详细学习教程
Single step debugging analysis of rxjs observable of operator
internal field separator
Fully annotated SSM framework construction
效率提升 - 鼓捣个性化容器开发环境
spark analyze命令使用及其作用 map join broadcast join 广播join
【QT小作】封装一个简单的线程管理类
Object memory layout
Operation category read is not supported in state standby








