[Swift]LeetCode1171. 從鏈表中刪去總和值爲零的連續節點 | Remove Zero Sum Consecutive Nodes from Linked List

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:爲敢(WeiGanTechnologies)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ndefhpoo-kc.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.node

After doing so, return the head of the final linked list.  You may return any such answer.git

 

(Note that in the examples below, all sequences are serializations of ListNode objects.)github

Example 1:微信

Input: head = [1,2,-3,3,1]
Output: [3,1]
Note: The answer [1,2,1] would also be accepted.

Example 2:app

Input: head = [1,2,3,-3,4]
Output: [1,2,4]

Example 3:spa

Input: head = [1,2,3,-3,-2]
Output: [1]

 

Constraints:code

  • The given linked list will contain between 1 and 1000 nodes.
  • Each node in the linked list has -1000 <= node.val <= 1000.

給你一個鏈表的頭節點 head,請你編寫代碼,反覆刪去鏈表中由 總和 值爲 0 的連續節點組成的序列,直到不存在這樣的序列爲止。htm

刪除完畢後,請你返回最終結果鏈表的頭節點。對象

 

你能夠返回任何知足題目要求的答案。

(注意,下面示例中的全部序列,都是對 ListNode 對象序列化的表示。)

示例 1:

輸入:head = [1,2,-3,3,1]
輸出:[3,1]
提示:答案 [1,2,1] 也是正確的。

示例 2:

輸入:head = [1,2,3,-3,4]
輸出:[1,2,4]

示例 3:

輸入:head = [1,2,3,-3,-2]
輸出:[1]

 

提示:

  • 給你的鏈表中可能有 1 到 1000 個節點。
  • 對於鏈表中的每一個節點,節點的值:-1000 <= node.val <= 1000.

Runtime: 28 ms
Memory Usage: 21.5 MB
 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12 class Solution {
13     func removeZeroSumSublists(_ head: ListNode?) -> ListNode? {
14         var head = head
15         if head == nil {return head}
16         var sums:[Int:ListNode] = [Int:ListNode]()
17         var sum:Int = 0
18         var curr:ListNode? = head
19         while(curr != nil)
20         {
21             sum += curr!.val
22             if sum == 0
23             {
24                 head = curr?.next
25             }
26             if sums[sum] != nil
27             {
28                 sums[sum]!.next = curr?.next
29             }
30             else
31             {
32                 sums[sum] = curr
33             }
34             curr = curr?.next
35         }
36         return head        
37     }
38 }

36ms

 

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12 class Solution {
13     func removeZeroSumSublists(_ head: ListNode?) -> ListNode? {
14                
15         var arr = [Int]()
16         var sumArr = [Int]()
17         var dic = [0: -1]
18         var head = head
19         var invaildRange = [(Int, Int)]()
20         var i = 0
21         while head != nil {
22             if head!.val == 0 {
23                 head = head!.next
24                 continue
25             }
26             arr.append(head!.val)
27             if sumArr.count == 0 {
28                 sumArr.append(head!.val)
29             } else {
30                 sumArr.append(sumArr[sumArr.count-1]+head!.val)
31             }
32             if dic[sumArr[sumArr.count-1]] != nil {
33                 let lasti = dic[sumArr[sumArr.count-1]]!
34                 invaildRange.append((lasti+1, i))
35                 for j in lasti..<i {
36                     dic[sumArr[j+1]] = nil
37                 } 
38                 dic[sumArr[sumArr.count-1]] = lasti
39             } else {
40                 dic[sumArr[sumArr.count-1]] = i
41             }
42             head = head!.next
43             i += 1
44         }
45         if sumArr.count == 0 || sumArr[sumArr.count-1] == 0 {
46             return nil
47         }
48 
49         // print(invaildRange)
50         
51         var hd: ListNode? = nil
52         var res: ListNode? = nil
53 
54         invaildRange.append((Int.max, Int.max))
55         invaildRange.sort {
56             if $0.0 < $1.0 || ($0.0 == $1.0 && $0.1 > $1.1 ) {
57                 return true
58             }
59             return false
60         }
61         var last = invaildRange.removeFirst()
62         for i in arr.indices {
63             while i > last.1 && invaildRange.count > 0 {
64                 last = invaildRange.removeFirst()
65             }
66             // print(i, last)
67             if i < last.0 {
68                 if hd == nil {
69                     hd = ListNode(arr[i])
70                     res = hd
71                 } else {
72                     hd!.next = ListNode(arr[i])
73                     hd = hd!.next
74                 }
75             }
76         }
77         return res 
78     }
79 }

44ms

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12 class Solution {
13     func removeZeroSumSublists(_ head: ListNode?) -> ListNode? { 
14         var arr = [Int](), sumArr = [Int]()
15         var dic = [0: -1]
16         var head = head
17         var invaildRange = [(Int, Int)]()
18         var i = 0
19         while head != nil {
20             //ignor zero element
21             if head!.val == 0 { head = head!.next; continue }
22             
23             arr.append(head!.val)
24             if sumArr.count == 0 { sumArr.append(head!.val) } 
25             else { sumArr.append(sumArr.last! + head!.val) }
26             
27             let lasti = dic[sumArr.last!] ?? i
28             if lasti != i {
29                 invaildRange.append((lasti+1, i))
30                 for j in lasti..<i {
31                     dic[sumArr[j+1]] = nil
32                 } 
33             } 
34             dic[sumArr.last!] = lasti
35             head = head!.next
36             i += 1
37         }
38         if sumArr.count == 0 || sumArr[sumArr.count-1] == 0 { return nil }
39         var hd: ListNode? = nil
40         var res: ListNode? = nil
41 
42         invaildRange.append((Int.max, Int.max))
43         invaildRange.sort { $0.0 < $1.0  }
44         var last = invaildRange.removeFirst()
45         for i in arr.indices {
46             while i > last.1 { last = invaildRange.removeFirst() }
47             if i < last.0 {
48                 if hd == nil {
49                     hd = ListNode(arr[i])
50                     res = hd
51                 } else {
52                     hd!.next = ListNode(arr[i])
53                     hd = hd!.next
54                 }
55             }
56         }
57         return res 
58     }
59 }
相關文章
相關標籤/搜索