Skip to content

Commit 536e2f2

Browse files
author
iamminji
committed
[20200706] Solve july challenge questions
1 parent 55251ac commit 536e2f2

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,5 @@
228228
| [Binary Tree Level Order Traversal II](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3378/) | [python](challenge/2020/july/Binary_Tree_Level_Order_Traversal_II.py) | [solutions](challenge/2020/july/Binary_Tree_Level_Order_Traversal_II.md)|
229229
| [Prison Cells After N Days](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3379/) | [python](challenge/2020/july/Prison_Cells_After_N_Days.py) | [solutions](challenge/2020/july/Prison_Cells_After_N_Days.md)|
230230
| [Ugly Number II](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3380/) | [python](challenge/2020/july/Ugly_Number_II.py) | [solutions](challenge/2020/july/Ugly_Number_II.md)|
231-
| [Hamming Distance](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3381/) | [python](challenge/2020/july/Hamming_Distance.py) | [solutions](challenge/2020/july/Hamming_Distance.md)|
231+
| [Hamming Distance](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3381/) | [python](challenge/2020/july/Hamming_Distance.py) | [solutions](challenge/2020/july/Hamming_Distance.md)|
232+
| [Plus One](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3382/) | [python](challenge/2020/july/Plus_One.py) | [solutions](challenge/2020/july/Plus_One.md)|

challenge/2020/july/Plus_One.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## 문제
2+
3+
Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
4+
5+
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.
6+
7+
You may assume the integer does not contain any leading zero, except the number 0 itself.
8+
9+
Example 1:
10+
11+
```
12+
Input: [1,2,3]
13+
Output: [1,2,4]
14+
Explanation: The array represents the integer 123.
15+
```
16+
17+
18+
Example 2:
19+
```
20+
Input: [4,3,2,1]
21+
Output: [4,3,2,2]
22+
Explanation: The array represents the integer 4321.
23+
```
24+
25+
### 솔루션
26+
리스트의 가장 마지막 값에 1을 더하는 문제다. list 를 string 으로 join 해서 다시 이걸 integer 로 변경 후 1을 더하고 다시 list 로 바꾸는 방법도 있겠지만..
27+
문제에서 원한 것은 그게 아니였을 것이다.
28+
29+
30+
리스트의 가장 마지막 값에 1을 더하고, 그 값이 10을 넘는 경우는 값을 올려야 하기 때문에 10으로 나눈 몫과 나머지를 각각 구한다.
31+
나머지 값은 다시 리스트에 넣고 몫은 다음 번 계산 때 사용한다.
32+
33+
마지막 몫이 0이 아닐 경우 리스트에 넣어주고 이 리스트를 뒤집어 주면 된다.
34+
35+
코드는 아래와 같다.
36+
37+
```python3
38+
class Solution:
39+
def plusOne(self, digits: List[int]) -> List[int]:
40+
result = list()
41+
n = 1
42+
for i in range(len(digits) - 1, -1, -1):
43+
num = digits[i] + n
44+
p, q = num // 10, num % 10
45+
result.append(q)
46+
n = p
47+
48+
if n != 0:
49+
result.append(n)
50+
51+
return result[::-1]
52+
```
53+
54+
#### 복잡도
55+
시간 복잡도는 O(n) 이고 공간 복잡도도 O(n) 이다.

challenge/2020/july/Plus_One.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def plusOne(self, digits: List[int]) -> List[int]:
6+
result = list()
7+
n = 1
8+
for i in range(len(digits) - 1, -1, -1):
9+
num = digits[i] + n
10+
p, q = num // 10, num % 10
11+
result.append(q)
12+
n = p
13+
14+
if n != 0:
15+
result.append(n)
16+
17+
return result[::-1]
18+
19+
20+
if __name__ == '__main__':
21+
sol = Solution()
22+
print(sol.plusOne([1, 2, 3, 4]))
23+
print(sol.plusOne([1, 2, 3, 9]))

0 commit comments

Comments
 (0)