[Swift]LeetCode1019. 鏈表中的下一個更大節點 | Next Greater Node In Linked List

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

We are given a linked list with head as the first node.  Let's number the nodes in the list: node_1, node_2, node_3, ... etc.node

Each node may have a next larger value: for node_inext_larger(node_i) is the node_j.val such that j > inode_j.val > node_i.val, and j is the smallest possible choice.  If such a j does not exist, the next larger value is 0.git

Return an array of integers answer, where answer[i] = next_larger(node_{i+1}).github

Note that in the example inputs (not outputs) below, arrays such as [2,1,5] represent the serialization of a linked list with a head node value of 2, second node value of 1, and third node value of 5. 數組

Example 1:微信

Input: [2,1,5]
Output: [5,5,0] 

Example 2:app

Input: [2,7,4,3,5]
Output: [7,0,5,5,0] 

Example 3:ide

Input: [1,7,5,1,9,2,5,1]
Output: [7,9,9,9,0,5,0,0] 

Note:this

  1. 1 <= node.val <= 10^9 for each node in the linked list.
  2. The given list has length in the range [0, 10000].

給出一個以頭節點 head 做爲第一個節點的鏈表。鏈表中的節點分別編號爲:node_1, node_2, node_3, ... 。spa

每一個節點均可能有下一個更大值(next larger value):對於 node_i,若是其 next_larger(node_i) 是 node_j.val,那麼就有 j > i 且  node_j.val > node_i.val,而 j 是可能的選項中最小的那個。若是不存在這樣的 j,那麼下一個更大值爲 0 。

返回整數答案數組 answer,其中 answer[i] = next_larger(node_{i+1}) 。

注意:在下面的示例中,諸如 [2,1,5] 這樣的輸入(不是輸出)是鏈表的序列化表示,其頭節點的值爲 2,第二個節點值爲 1,第三個節點值爲 5 。 

示例 1:

輸入:[2,1,5]
輸出:[5,5,0]

示例 2:

輸入:[2,7,4,3,5]
輸出:[7,0,5,5,0]

示例 3:

輸入:[1,7,5,1,9,2,5,1]
輸出:[7,9,9,9,0,5,0,0] 

提示:

  1. 對於鏈表中的每一個節點,1 <= node.val <= 10^9
  2. 給定列表的長度在 [0, 10000] 範圍內

552ms
 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 nextLargerNodes(_ head: ListNode?) -> [Int] {
14         guard let head = head else { return [] }
15         var result = [0]
16         var stack = [(0, head)]
17         while let (lastIdx, lastNode) = stack.last, let curr = lastNode.next {
18             result.append(0)
19             if curr.val > lastNode.val {
20                 while stack.count > 0 {
21                     if let (idx, listNode) = stack.last, curr.val > listNode.val {
22                         stack.removeLast()
23                         result[idx] = curr.val
24                     } else {
25                         break
26                     }
27                 }
28             }
29             stack.append((lastIdx + 1, curr))
30         }
31         return result
32     }
33 }

604ms

 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 nextLargerNodes(_ head: ListNode?) -> [Int] {
14         guard let head = head else { return [] }
15         
16         var results = [Int]()
17         var curr: ListNode? = head
18         while curr != nil {
19             results.append(0)
20             curr = curr?.next
21         }
22         
23         var stack = [(Int, Int)]()
24         
25         curr = head
26         var i = 0
27         while let this = curr {
28             while let last = stack.last, this.val > last.1 {
29                 stack.removeLast()
30                 results[last.0] = this.val
31             }
32             
33             stack.append((i, this.val))
34             i += 1
35             curr = this.next
36         }
37         
38         return results
39     }
40 }

636ms

 1 class Solution {
 2     func nextLargerNodes(_ head: ListNode?) -> [Int] {
 3         var arr = [Int]()
 4         var node = head
 5         while let n = node {
 6             arr.append(n.val)
 7             node = n.next
 8         }
 9         var stack = Stack()
10         var ans = [Int](repeating: 0, count: arr.count)
11         for (index, num) in arr.enumerated().reversed() {
12             while let top = stack.top(),
13                 top <= num {
14                     stack.pop()
15             }
16             if let top = stack.top() {
17                 ans[index] = top
18             }
19             stack.push(num)
20         }
21         return ans
22     }
23 }
24 
25 struct Stack {
26     private var arr = [Int]()
27     init() {}
28     var count: Int {
29         return arr.count
30     }
31     var isEmpty: Bool {
32         return arr.isEmpty
33     }
34     mutating func push(_ n: Int) {
35         arr.append(n)
36     }
37     func top() -> Int? {
38         return arr.last
39     }
40     mutating func pop() -> Int? {
41         if isEmpty { return nil }
42         return arr.removeLast()
43     }
44 }

644ms

 1 class Solution {
 2     func length(_ head:ListNode?) -> Int
 3     {
 4         var curr = head
 5         var count = 0
 6         while curr != nil
 7         {
 8             curr = curr?.next
 9             count += 1
10         }
11         return count
12     }
13     
14     func nextLargerNodes(_ head: ListNode?) -> [Int] {
15         if head == nil
16         {
17             return []
18         }
19         if head?.next == nil
20         {
21             return [0]
22         }
23         
24         var curr = head
25         
26         var len = length(head)
27         
28         var list : [Int] = []
29         
30         var stk = Stack<ListNode>()
31         
32         while curr != nil
33         {
34             while !stk.isEmpty() && stk.peek().val < curr!.val
35             {
36                 var poppedItem = stk.pop()
37                 poppedItem.val = curr!.val
38             }
39             stk.push(curr!)
40             curr = curr?.next
41         }
42         
43         while !stk.isEmpty()
44         {
45             stk.pop().val = 0
46         }
47         
48         curr = head
49         while curr != nil
50         {
51             list.append(curr!.val)
52             curr = curr?.next
53         }
54         return list
55     }
56 }
57 
58 struct Stack<T>
59 {
60     var elements:[T] = []
61     mutating func push(_ item:T)
62     {
63         elements.append(item)
64     }
65     mutating func pop() -> T
66     {
67        return elements.removeLast() 
68     }
69     func isEmpty() -> Bool
70     {
71         return elements.isEmpty
72     }
73     func peek() -> T
74     {
75        return elements.last!
76     }
77 }

Runtime: 764 ms
Memory Usage: 21.3 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     var ret:[Int] = [Int]()
14     var d:[(Int,Int)] = [(Int,Int)]()
15     var ans:[Int] = [Int]()
16     
17     func nextLargerNodes(_ head: ListNode?) -> [Int] {
18         var head = head
19         while(head != nil)
20         {
21             ret.append(head!.val)
22             head = head?.next
23         }
24         for i in stride(from:ret.count - 1,through:0,by:-1)
25         {
26             while(d.count != 0 && d.first!.0 <= ret[i])
27             {
28                 
29                 d.removeFirst()                
30             }
31             if d.count != 0
32             {
33                 ans.append(d.first!.0)
34             }
35             else
36             {
37                 ans.append(0)
38             }
39             d.insert((ret[i],i),at:0)
40         }
41         return ans.reversed()
42     }
43 }
相關文章
相關標籤/搜索