-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusOne.java
More file actions
78 lines (72 loc) · 1.57 KB
/
PlusOne.java
File metadata and controls
78 lines (72 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.leetcode.editor.cn;;
//给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。
//
// 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。
//
// 你可以假设除了整数 0 之外,这个整数不会以零开头。
//
//
//
// 示例 1:
//
//
//输入:digits = [1,2,3]
//输出:[1,2,4]
//解释:输入数组表示数字 123。
//
//
// 示例 2:
//
//
//输入:digits = [4,3,2,1]
//输出:[4,3,2,2]
//解释:输入数组表示数字 4321。
//
//
// 示例 3:
//
//
//输入:digits = [0]
//输出:[1]
//
//
//
//
// 提示:
//
//
// 1 <= digits.length <= 100
// 0 <= digits[i] <= 9
//
// Related Topics 数组 数学
// 👍 866 👎 0
/**
* 66 加一
* @date 2021-11-30 11:02:36
* @author shang.liang
*/
public class PlusOne{
public static void main(String[] args) {
Solution solution = new PlusOne().new Solution();
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
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 中所有的元素均为 9
int[] ans = new int[n + 1];
ans[0] = 1;
return ans;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}