File tree Expand file tree Collapse file tree 2 files changed +38
-8
lines changed
project/LeetCode/leetcode/src/main/java/com/blankj/easy/_066 Expand file tree Collapse file tree 2 files changed +38
-8
lines changed Original file line number Diff line number Diff line change @@ -13,10 +13,28 @@ The digits are stored such that the most significant digit is at the head of the
1313
1414## 思路
1515
16- 题意是
16+ 题意是给你一个数字数组,高位在前,并且首位不为0除非这个数组就是 ` [0] ` ,让你给该数组低位加一求其结果,那么我们就模拟小学数学那样进位去算即可,如果一直进位到首位,这种情况也就是都是由9组成的数组,此时我们只要new出一个多一个长度的数组即可,并把第0个元素赋1即可。
1717
1818``` java
19-
19+ public class Solution {
20+ public int [] plusOne (int [] digits ) {
21+ int p = digits. length - 1 ;
22+ if (digits[p] < 9 ) {
23+ digits[p] = ++ digits[p];
24+ } else {
25+ do {
26+ digits[p-- ] = 0 ;
27+ } while (p >= 0 && digits[p] == 9 );
28+ if (digits[0 ] != 0 ) {
29+ ++ digits[p];
30+ } else {
31+ digits = new int [digits. length + 1 ];
32+ digits[0 ] = 1 ;
33+ }
34+ }
35+ return digits;
36+ }
37+ }
2038```
2139
2240
Original file line number Diff line number Diff line change 1313
1414public class Solution {
1515 public int [] plusOne (int [] digits ) {
16-
16+ int p = digits .length - 1 ;
17+ if (digits [p ] < 9 ) {
18+ digits [p ] = ++digits [p ];
19+ } else {
20+ do {
21+ digits [p --] = 0 ;
22+ } while (p >= 0 && digits [p ] == 9 );
23+ if (digits [0 ] != 0 ) {
24+ ++digits [p ];
25+ } else {
26+ digits = new int [digits .length + 1 ];
27+ digits [0 ] = 1 ;
28+ }
29+ }
1730 return digits ;
1831 }
1932
2033 public static void main (String [] args ) {
2134 Solution solution = new Solution ();
22- File file = new File ("/dev/d/" );
23- System .out .println (file .getAbsolutePath ());
24- System .out .println ("hello" .substring (0 , 2 ));
25- int [] digits = new int []{9 , 9 , 9 };
26- System .out .println (solution .plusOne (digits ));
35+ int [] digits = solution .plusOne (new int []{9 , 9 , 9 });
36+ for (int i = 0 ; i < digits .length ; i ++) {
37+ System .out .print (digits [i ]);
38+ }
2739 }
2840}
You can’t perform that action at this time.
0 commit comments