
๋ฌธ์ ํด๊ฒฐ ๊ณผ์
โ๏ธ ๋๋ง์ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ์ ๋ฆฌํ๊ธฐ (=์ฒด๊ณํ)
https://leetcode.com/problems/add-two-numbers/?envType=study-plan-v2&envId=top-interview-150
Add Two Numbers - LeetCode
Can you solve this real interview question? Add Two Numbers - You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and
leetcode.com
๋ ๊ฐ์ ๋งํฌ๋ ๋ฆฌ์คํธ๊ฐ ์ฃผ์ด์ง๋ค. ์ฃผ์ด์ง ๋งํฌ๋ ๋ฆฌ์คํธ์ ํฉ์ ๊ณ์ฐํ์ฌ ๋ ๋ค๋ฅธ ๋งํฌ๋ ๋ฆฌ์คํธ๋ก ๋ฐํํ๋ผ.
- ๋งํฌ๋ ๋ฆฌ์คํธ์ ์ซ์๋ ์ญ์์ผ๋ก ์ ์ฅ๋๋ค. ex. 2->4->3 : 342
- ๊ฐ ์ฐ๊ฒฐ๋ฆฌ์คํธ์ head ๋ ธ๋๋ 0์ด ์๋๋ค.
- ๊ฐ ์ฐ๊ฒฐ๋ฆฌ์คํธ์ ๋ ธ๋ ์๋ [1, 100]์ด๋ค.
- Node.val์ [0, 9]์ด๋ค.
๐ ์ด๋ค ์์ผ๋ก ์ ๊ทผํ๋๊ฐ?
๋งํฌ๋ ๋ฆฌ์คํธ์ ์ซ์๊ฐ ์ญ์์ผ๋ก ์ ์ฅ๋๋ค. (head์์ tail ๋ฐฉํฅ์ผ๋ก๋ง ์ฐ๊ฒฐ๋์ด ์์ผ๋ฏ๋ก ์ญ์์ผ๋ก ํ์ํ ์๋ ์๋ค.)
๋ฐ๋ผ์ head๋ถํฐ ์ฐจ๋ก๋ก ์ซ์๋ฅผ ๋ง๋ค์ด๊ฐ ๋ค ๋ง์ง๋ง์ reverse ํด์ฃผ์ด ํฉ์ ๊ณ์ฐํ๋ค.
๐ ์๊ณ ๋ฆฌ์ฆ ์ ํ: ์ ๋ ฅ์ ํฌ๊ธฐ๋ฅผ ๋ฐํ์ผ๋ก
์ฃผ์ด์ง ๋งํฌ๋ ๋ฆฌ์คํธ์ ์์๋ฅผ ํ๋์ฉ ์ํํด์ผ ํ๋ฏ๋ก linear search๋ฅผ ์ฌ์ฉํ๋ค.
๐ ์์ฌ ์ฝ๋
head = l1
while head๊ฐ None์ด ์๋ ๋์:
head.val์ L1์ ๋์ ํ๋ค
head์ head.next๋ฅผ ๋์
ํ๋ค
head = l2
while head๊ฐ None์ด ์๋ ๋์:
head.val์ L2์ ๋์ ํ๋ค
head์ head.next๋ฅผ ๋์
ํ๋ค
int(L1[::-1])๊ณผ int(L2[::-1])๋ฅผ ํฉํ๋ค.
๐ฏ ๊ตฌํ ์ฝ๋
๋ ๊ฐ์ ๋งํฌ๋ ๋ฆฌ์คํธ์ ํฉ์ ๊ณ์ฐํ๋ ๊ฒ๊น์ง ๊ตฌํํ์์ผ๋ ๋ ๋ค๋ฅธ ๋งํฌ๋ ๋ฆฌ์คํธ๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ ๋ชจ๋ฅด๊ฒ ๋ค.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
L1, L2 = '', '' # ๊ฐ ์ฐ๊ฒฐ๋ฆฌ์คํธ์ ๋ด๊ธด ์๋ฅผ ์ ์ฅํ ๋ฌธ์์ด (์ญ์์ผ๋ก ํฉํ๊ธฐ ์ํด ๋ฌธ์์ด์ ์ ์ฅํ๋ค)
head = l1
while head != None:
L1 += str(head.val)
head = head.next # ํ ์นธ ์ด๋
head = l2
while head != None:
L2 += str(head.val)
head = head.next # ํ ์นธ ์ด๋
# ๋งํฌ๋ ๋ฆฌ์คํธ์ ์ซ์๊ฐ ์ญ์์ผ๋ก ์ ์ฅ๋์ด ์์์ผ๋, reverse ํ์ฌ ํฉํ๋ค.
ans = int(L1[::-1]) + int(L2[::-1])
๋ด ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
l1๊ณผ l2์ ๊ธธ์ด๋ฅผ N์ด๋ผ๊ณ ํ ๋, ์๊ฐ ๋ณต์ก๋๋ O(N)์ด๋ค.
โ๏ธ ์ฑ๋ฅ ํ๊ณ
N์ด ์ต๋ 100์ด๋ฏ๋ก O(N)์ผ๋ก ๊ฑฐ๋ฌํ๋ค.
๐ก ๋ฌด์์/์ด๋ป๊ฒ ๊ฐ์ ํ ์ ์์๊น?
๊ฐ ๋งํฌ๋ ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ฉฐ ์ซ์๋ฅผ ๋ง๋ค ๋, ๋ฌธ์์ด๊ณผ ์ซ์์ ๋ณํ์ด ์ฆ์ ํท๊ฐ๋ฆฐ๋ค.
์ฒ์๋ถํฐ ์ซ์๋ก ๋ง๋ค ์๋ ์์๊น.
๋ค๋ฅธ ์ฌ๋์ ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
๐ฏ ๊ตฌํ ์ฝ๋
L1๊ณผ L2์ ํ์ ์ ๋ฌธ์์ด์ด ์๋ ์ ์๋ก ์ง์ ํ๋ค.
์ฐ๊ฒฐ๋ฆฌ์คํธ๋ head์ linked_lst๋ฅผ ์ ์ธํ ๋ค cur ๋ ธ๋๋ฅผ ์์ฑํ์ฌ ํ๋์ฉ ์ด์ด๋ถ์ธ๋ค.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
L1, L2 = 0, 0
tmp = 1
head = l1
while head != None:
L1 += head.val * tmp
head = head.next # ๋
ธ๋ ํ ์นธ ์ด๋
tmp *= 10 # โ
์๋ฆฌ ์ ์ด๋
tmp = 1
head = l2
while head != None:
L2 += head.val * tmp
head = head.next # ๋
ธ๋ ํ ์นธ ์ด๋
tmp *= 10 # โ
์๋ฆฌ ์ ์ด๋
ans = L1 + L2
# โ
์๋ก์ด ์ฐ๊ฒฐ๋ฆฌ์คํธ ์์ฑ
head = None
linked_lst = None
for x in str(ans):
if not head:
head = ListNode(int(x))
linked_lst = head
else:
cur = ListNode(int(x), linked_lst) # โ
cur ๋
ธ๋์ next์ ๊ธฐ์กด ๋
ธ๋(linked_lst)๋ฅผ ์ฐ๊ฒฐํ๋ค
linked_lst = cur # โ
๊ฐฑ์
return linked_lst
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
๋ง์ฐฌ๊ฐ์ง๋ก O(N)์ ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ๋๋ค.
๋๋ ์
๋งํฌ๋ ๋ฆฌ์คํธ๋ฅผ ํ์ํ๋ ๊ฒ์ ์ต์ํ์ผ๋ ์๋ก์ด ๋งํฌ๋ ๋ฆฌ์คํธ๋ฅผ ์์ฑํ๋ ๋ถ๋ถ์ด ๋ฏ์ค์๋ค.
ํ์ง๋ง ๊ตฌํ์ ์๊ฐ๋ณด๋ค ๋จ์ํ๋ค. ์ ์ํ ์ ์๋๋ก ํด์ผ๊ฒ ๋ค.
'๐ซ ETC > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Week 1-2. [Stack] 155. Min Stack (Python3) (0) | 2023.08.29 |
|---|---|
| Week 1-2. [Linked List] 21. Merge Two Sorted Lists (Python3) (0) | 2023.08.29 |
| Week 1-2. [Linked List] 141. Linked List Cycle (Python3) (0) | 2023.08.28 |
| ๐ก Week 1-2. [Sliding Window] 3. Longest Substring Without Repeating Characters (Python3) (0) | 2023.08.28 |
| Week 1-2. [Sliding Window] 209. Minimum Size Subarray Sum (Java) (0) | 2023.08.28 |