-
[LeetCode] 2. Add Two Numbers / Swift프로그래밍/코딩테스트 2021. 6. 29. 19:34
[문제 보기]
더보기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.
https://leetcode.com/problems/add-two-numbers/
첫번째 방법
// 첫번째 버전 /** * Definition for singly-linked list. * public class ListNode { * public var val: Int * public var next: ListNode? * public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val; self.next = nil; } * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } * } */ class Solution { func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { var cur1 = l1 var cur2 = l2 var buffer = 0 let start = ListNode() var ansCur = start while cur1 != nil && cur2 != nil { ansCur.next = ListNode() ansCur = ansCur.next! let val = cur1!.val + cur2!.val + buffer ansCur.val = val % 10 buffer = val / 10 cur1 = cur1?.next cur2 = cur2?.next } if cur1 == nil && cur2 == nil && buffer == 0 { return start.next } var p = cur1 if p == nil { p = cur2 } while p != nil || buffer > 0 { let val = (p?.val ?? 0) + buffer ansCur.next = ListNode(val % 10) ansCur = ansCur.next! p = p?.next buffer = val / 10 } return start.next } }
첫번째 방법에서 수정한 방법:
기존 방법: while문에서 두개의 커서가 모두 nil이 아닐때까지 반복
수정한 방법: while문에서 두개의 커서가 모두 nil이 될때까지 반복. 이 방법은 while문 종료후에 nil이 아닌 어느 한쪽을 따로 이어주는 부분이 없음. 수행시간은 두번째 방법(52ms)보다 첫번째 방법(44ms)이 더 좋았음
// 두번째 버전 /** * Definition for singly-linked list. * public class ListNode { * public var val: Int * public var next: ListNode? * public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val; self.next = nil; } * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } * } */ class Solution { func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { var cur1 = l1 var cur2 = l2 var buffer = 0 let start = ListNode() var ansCur = start while cur1 != nil || cur2 != nil { ansCur.next = ListNode() ansCur = ansCur.next! let val = (cur1?.val ?? 0) + (cur2?.val ?? 0) + buffer ansCur.val = val % 10 buffer = val / 10 cur1 = cur1?.next cur2 = cur2?.next } if buffer > 0 { ansCur.next = ListNode(buffer) } return start.next } }
반응형'프로그래밍 > 코딩테스트' 카테고리의 다른 글
[LeetCode] 5. Longest Palindromic Substring / Swift (0) 2021.07.01 [LeetCode] 3. Longest Substring Without Repeating Characters / Swift (0) 2021.06.30 [LeetCode] 543. Diameter of Binary Tree / Swift (0) 2021.06.29 [LeetCode] 1047. Remove All Adjacent Duplicates In String / Swift (0) 2021.06.29 [LeetCode] 338. Counting Bits / Swift (0) 2021.06.26