Leetcode :: Add Two Numbers

์ˆ‘์ˆ‘ยท2021๋…„ 5์›” 12์ผ
0

์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ชฉ๋ก ๋ณด๊ธฐ
90/122
post-thumbnail

Problem

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 return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example 1:

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

Example 2:

Input: l1 = [0], l2 = [0]
Output: [0]

Example 3:

Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]

Constraints:

  • The number of nodes in each linked list is in the range [1, 100].
  • 0 <= Node.val <= 9
  • It is guaranteed that the list represents a number that does not have leading zeros.

Guess

  • ์™ ์ง€ ๋ณด๊ธฐ ๋“œ๋ฌธ ์œ ํ˜•์ธ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋‹ค.
  • ๊ทธ๋ƒฅ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋ฉด ๋˜๋Š”..๋ฐ
  • ํด๋ž˜์Šคํ™” ๋˜์–ด์žˆ์œผ๋ฏ€๋กœ ์žฌ๊ท€๋ฅผ ํ™œ์šฉํ•ด ํƒ€์ž…์„ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

Solution 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: ListNode, l2: ListNode) -> ListNode:
        
        ans1, ans2 = [], []
        
            
        def dfs(head, ans):
            if head == None:
                return

            ans.append(head.val)
            dfs(head.next, ans)

        def makeListNode(prev, idx):
            if idx == len(ans):
                prev.next = None
                answer = prev
                return
                
            tmp = ListNode(ans[idx])
            prev.next = tmp
            answer = prev
            makeListNode(tmp, idx+1)

        dfs(l1, ans1)
        dfs(l2, ans2)

        ans1.reverse()
        ans2.reverse()

        ans1 = "".join(list(map(str, ans1)))
        ans2 = "".join(list(map(str, ans2)))
        
        ans = list(str(int(ans1) + int(ans2)))
        ans.reverse()
      
      	
        head = ListNode(ans[0])
        answer = head
        makeListNode(head, 1)
        
        return answer

์ด๊ฑด ๋ญ.. ์ฒ˜์ฐธํ•˜๋‹ค...
์žฌ๊ท€๋ฅผ ๋Œ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ์— ๋น„ํ•ด ์Šคํƒ ๊ณต๊ฐ„์„ ๋งŽ์ด ์ฐจ์ง€ํ•  ๊ฑฐ๊ณ  + ํƒ€์ž… ๋ณ€ํ™˜์„ ์—ฌ๋Ÿฌ๋ฒˆ ํ•˜๋‹ค๋ณด๋‹ˆ ๋‹น์—ฐํ•œ ๊ฒฐ๊ณผ๋‹ค.

carry ๋ฅผ ํ™œ์šฉํ•ด์„œ ์ดˆ๋“ฑ์ˆ˜ํ•™ ๋ง์…ˆ์„ ๊ตฌํ˜„ํ•˜๋Š”๊ฒŒ ์ •ํ•ด์˜€๋‹ค.
์ธ์‚ฌ์ดํŠธ๋ฅผ ์–ป์—ˆ์œผ๋ฏ€๋กœ ๋‹ค์‹œ ํ’€์–ด๋ณด์ž.

Solution 1

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        answer = ListNode()
        head = answer
        carry = 0
        
        while l1 or l2 or carry:
            val1 = l1.val if l1 else 0
            val2 = l2.val if l2 else 0
            carry, res = divmod(val1 + val2 + carry, 10)
            
            head.next = ListNode(res)
            head = head.next
            
            l1 = l1.next if l1 else None
            l2 = l2.next if l2 else None
            
        return answer.next

ํšŒ๊ณ 

  • ๋‹จ์ˆœํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด ์˜คํžˆ๋ ค ์‰ฌ์šด ๋ฌธ์ œ์˜€๋‹ค.

  • ๋ฉ”์†Œ๋“œ๋ฅผ ์ž๊พธ ์ด์šฉํ•˜๋ ค๊ณ  ์ƒ๊ฐํ•˜๋‹ค ๋ณด๋‹ˆ ๋ถˆํ•„์š”ํ•œ ํƒ€์ž… ๋ณ€ํ™˜, ์žฌ๊ท€ ์—ฐ์‚ฐ ๋“ฑ๋“ฑ์ด ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

  • ์กฐ๊ธˆ ๋” ์‹ ์ค‘ํžˆ ์ƒ๊ฐํ•ด๋ณด๊ณ  ํ’€์–ด๋„ ๊ดœ์ฐฎ๋‹ค.

profile
ํˆด ๋งŒ๋“ค๊ธฐ ์ข‹์•„ํ•˜๋Š” ์‚ฝ์งˆ ์ „๋ฌธ(...) ์ฃผ๋‹ˆ์–ด ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€