Skip to content

Commit 940deac

Browse files
committed
0066. Plus One
1 parent f1611b9 commit 940deac

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

markdown/0066. Plus One.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
### [66\. Plus One](https://leetcode.com/problems/plus-one/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given a **non-empty** array of digits representing a non-negative integer, plus one to the integer.
7+
8+
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
9+
10+
You may assume the integer does not contain any leading zero, except the number 0 itself.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: [1,2,3]
16+
Output: [1,2,4]
17+
Explanation: The array represents the integer 123.
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: [4,3,2,1]
24+
Output: [4,3,2,2]
25+
Explanation: The array represents the integer 4321.
26+
```
27+
28+
29+
#### Solution
30+
31+
Language: **Java**
32+
33+
```java
34+
class Solution {
35+
   public int[] plusOne(int[] digits) {
36+
       if (digits == null || digits.length == 0) {
37+
           int[] result = {1};
38+
           return result;
39+
      }
40+
       int plusNum = 1;
41+
       for (int i = digits.length - 1; i >= 0 && plusNum > 0; i--) {
42+
           int newNum = digits[i] + plusNum;
43+
           plusNum = newNum / 10;
44+
           digits[i] = newNum % 10;
45+
      }
46+
       if (plusNum > 0) {
47+
           int[] result = new int[digits.length + 1];
48+
           result[0] = plusNum;
49+
           for (int i = 0; i < digits.length; i++) {
50+
               result[i + 1] = digits[i];
51+
          }
52+
           return result;
53+
      }
54+
       return digits;
55+
  }
56+
}
57+
```
58+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190719172019.png)

src/main/java/leetcode/_66_/Main.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode._66_;
2+
3+
import leetcode.common.Printer;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
int[] digits = {1, 2, 3};
12+
int[] ints = solution.plusOne(digits);
13+
Printer.printArrays(ints);
14+
}
15+
}
16+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package leetcode._66_;
2+
3+
class Solution {
4+
public int[] plusOne(int[] digits) {
5+
if (digits == null || digits.length == 0) {
6+
int[] result = {1};
7+
return result;
8+
}
9+
int plusNum = 1;
10+
for (int i = digits.length - 1; i >= 0 && plusNum > 0; i--) {
11+
int newNum = digits[i] + plusNum;
12+
plusNum = newNum / 10;
13+
digits[i] = newNum % 10;
14+
}
15+
if (plusNum > 0) {
16+
int[] result = new int[digits.length + 1];
17+
result[0] = plusNum;
18+
for (int i = 0; i < digits.length; i++) {
19+
result[i + 1] = digits[i];
20+
}
21+
return result;
22+
}
23+
return digits;
24+
}
25+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
### [66\. Plus One](https://leetcode.com/problems/plus-one/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given a **non-empty** array of digits representing a non-negative integer, plus one to the integer.
7+
8+
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
9+
10+
You may assume the integer does not contain any leading zero, except the number 0 itself.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: [1,2,3]
16+
Output: [1,2,4]
17+
Explanation: The array represents the integer 123.
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: [4,3,2,1]
24+
Output: [4,3,2,2]
25+
Explanation: The array represents the integer 4321.
26+
```
27+
28+
29+
#### Solution
30+
31+
Language: **Java**
32+
33+
```java
34+
class Solution {
35+
   public int[] plusOne(int[] digits) {
36+
       if (digits == null || digits.length == 0) {
37+
           int[] result = {1};
38+
           return result;
39+
      }
40+
       int plusNum = 1;
41+
       for (int i = digits.length - 1; i >= 0 && plusNum > 0; i--) {
42+
           int newNum = digits[i] + plusNum;
43+
           plusNum = newNum / 10;
44+
           digits[i] = newNum % 10;
45+
      }
46+
       if (plusNum > 0) {
47+
           int[] result = new int[digits.length + 1];
48+
           result[0] = plusNum;
49+
           for (int i = 0; i < digits.length; i++) {
50+
               result[i + 1] = digits[i];
51+
          }
52+
           return result;
53+
      }
54+
       return digits;
55+
  }
56+
}
57+
```
58+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190719172019.png)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode.common;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-07-19.
5+
*/
6+
public class Printer {
7+
public static void printArrays(int[] arrays) {
8+
if (arrays == null) {
9+
System.out.println("[]");
10+
return;
11+
}
12+
System.out.print("[");
13+
for (int i = 0; i < arrays.length - 1; i++) {
14+
System.out.print(arrays[i] + ",");
15+
}
16+
System.out.print(arrays[arrays.length - 1] + "]");
17+
}
18+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy