[Swift]LeetCode735. 行星碰撞 | Asteroid Collision

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

We are given an array asteroids of integers representing asteroids in a row.git

For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.github

Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.數組

Example 1:微信

Input: 
asteroids = [5, 10, -5]
Output: [5, 10]
Explanation: 
The 10 and -5 collide resulting in 10.  The 5 and 10 never collide. 

Example 2:app

Input: 
asteroids = [8, -8]
Output: []
Explanation: 
The 8 and -8 collide exploding each other. 

Example 3:ide

Input: 
asteroids = [10, 2, -5]
Output: [10]
Explanation: 
The 2 and -5 collide resulting in -5.  The 10 and -5 collide resulting in 10. 

Example 4:oop

Input: 
asteroids = [-2, -1, 1, 2]
Output: [-2, -1, 1, 2]
Explanation: 
The -2 and -1 are moving left, while the 1 and 2 are moving right.
Asteroids moving the same direction never meet, so no asteroids will meet each other. 

Note:spa

  • The length of asteroids will be at most 10000.
  • Each asteroid will be a non-zero integer in the range [-1000, 1000].

給定一個整數數組 asteroids,表示在同一行的行星。code

對於數組中的每個元素,其絕對值表示行星的大小,正負表示行星的移動方向(正表示向右移動,負表示向左移動)。每一顆行星以相同的速度移動。

找出碰撞後剩下的全部行星。碰撞規則:兩個行星相互碰撞,較小的行星會爆炸。若是兩顆行星大小相同,則兩顆行星都會爆炸。兩顆移動方向相同的行星,永遠不會發生碰撞。

示例 1:

輸入: 
asteroids = [5, 10, -5]
輸出: [5, 10]
解釋: 
10 和 -5 碰撞後只剩下 10。 5 和 10 永遠不會發生碰撞。

示例 2:

輸入: 
asteroids = [8, -8]
輸出: []
解釋: 
8 和 -8 碰撞後,二者都發生爆炸。

示例 3:

輸入: 
asteroids = [10, 2, -5]
輸出: [10]
解釋: 
2 和 -5 發生碰撞後剩下 -5。10 和 -5 發生碰撞後剩下 10。

示例 4:

輸入: 
asteroids = [-2, -1, 1, 2]
輸出: [-2, -1, 1, 2]
解釋: 
-2 和 -1 向左移動,而 1 和 2 向右移動。
因爲移動方向相同的行星不會發生碰撞,因此最終沒有行星發生碰撞。

說明:

  • 數組 asteroids 的長度不超過 10000
  • 每一顆行星的大小都是非零整數,範圍是 [-1000, 1000] 。

84ms

 1 class Solution {
 2     func asteroidCollision(_ asteroids: [Int]) -> [Int] {
 3         var positiveItems = [Int]()
 4         var negativeItems = [Int]()
 5         var isAddNegative = false
 6          
 7         for asteroid in asteroids
 8         {
 9             if asteroid > 0 
10             {
11                 positiveItems.append(asteroid)
12             }
13             else
14             {
15               isAddNegative = true
16               while positiveItems.count > 0
17               {
18                   
19                 if positiveItems.last! > abs(asteroid)
20                 {
21                     isAddNegative = false
22                     break
23                 }
24                 else 
25                 {
26                     let lastPositiveItem = positiveItems.removeLast()
27 
28                     if lastPositiveItem == abs(asteroid)
29                     {
30                         isAddNegative = false
31                         break
32 
33                     }                    
34                   }
35                 }
36                 
37                 if isAddNegative
38                 {
39                      negativeItems.append(asteroid)
40                 }
41             }            
42         }
43         
44         return negativeItems + positiveItems
45         
46     }
47 }

Runtime: 88 ms
Memory Usage: 19.2 MB
 1 class Solution {
 2     struct Stack<T> {
 3         private var temperatures = [T]()
 4         mutating func push(_ item:T){
 5             self.temperatures.append(item)
 6         }
 7         
 8         mutating func pop()->T?{
 9             return self.temperatures.popLast()
10         }
11         
12         func isEmpty() -> Bool {
13             return self.temperatures.isEmpty
14         }
15         
16         func peek() -> T? {
17            return self.temperatures.last
18         }
19         
20         func contents() -> [T] {
21             return self.temperatures
22         }
23     }
24     
25         func asteroidCollision(_ asteroids: [Int]) -> [Int] {
26         
27         var stack:Stack = Stack<Int>()
28         
29         for asteroid in asteroids {
30             var isShouldPush = true
31             if asteroid < 0 {
32                 // 向左移動,碰撞檢測
33                 while let peek = stack.peek(),peek > 0 {
34                     if abs(peek) <= abs(asteroid) {
35                         stack.pop()
36                         isShouldPush = abs(asteroid) - abs(peek) > 0 ? true : false
37                     }else{
38                         isShouldPush = false
39                     }
40                     
41                     if isShouldPush == false{
42                         break
43                     }
44                 }
45                 
46             }
47             
48             if isShouldPush {
49                 stack.push(asteroid)
50             }
51         }
52         return stack.contents()
53     }
54 }

88ms

 1 class Solution {
 2     func asteroidCollision(_ asteroids: [Int]) -> [Int] {
 3         var output = [Int]()
 4         
 5         for index in 0..<asteroids.count {
 6             let rock = asteroids[index]
 7             if rock > 0 {
 8                 output.append(rock)
 9                 continue
10             }
11             checkExplosion(&output, rock)
12         }
13         return output
14     }
15     
16     func checkExplosion(_ output: inout [Int], _ rock: Int) {
17         if let last = output.last {
18             if last < 0 {
19                 output.append(rock)
20             } else {
21                 output.removeLast()
22                 // Explode
23                 if abs(last) > abs(rock) {
24                     output.append(last)
25                 } else if abs(last) < abs(rock) {
26                     checkExplosion(&output, rock)
27                 }
28             }
29         } else {
30             output.append(rock)
31         }
32     }
33 }

96ms

 1 class Solution {
 2     func asteroidCollision(_ asteroids: [Int]) -> [Int] {
 3         var positives = [Int]()
 4         var negatives = [Int]()
 5         
 6         for asteroid in asteroids {
 7             if asteroid > 0 {
 8                 positives.append(asteroid)
 9             } else {
10                 var shouldAppendToNegative = true
11                 
12                 while positives.count > 0 {
13                     if positives.last! > -asteroid {
14                         shouldAppendToNegative = false
15                         break
16                     } else {
17                         let last = positives.removeLast()
18                         
19                         if -asteroid == last {
20                             shouldAppendToNegative = false
21                             break
22                         }
23                     }
24                 }
25                 
26                 if shouldAppendToNegative {
27                     negatives.append(asteroid)
28                 }
29             }
30         }
31         
32         return negatives + positives
33     }
34 }

100ms

 1 class Solution {
 2     func asteroidCollision(_ asteroids: [Int]) -> [Int] {
 3         var goR = [Int]()
 4         var result = [Int]()
 5         
 6         for ast in asteroids {
 7             if ast < 0 {
 8                 var astExploded = false
 9                 while !goR.isEmpty && !astExploded {
10                     if let prev = goR.last {
11                         if prev.magnitude == ast.magnitude {
12                             goR.removeLast()
13                             astExploded = true
14                         } else if prev.magnitude < ast.magnitude {
15                             goR.removeLast()
16                         } else {
17                             astExploded = true
18                         }
19                     } else {
20                         result.append(ast)
21                     }
22                 }
23                 if !astExploded {
24                     result.append(ast)
25                 }
26             } else {
27                 goR.append(ast)
28             }
29         }
30 
31         if !goR.isEmpty {
32             result.append(contentsOf: goR)
33         }
34         return result
35     }
36 }

112ms

 1 class Solution {
 2     func asteroidCollision(_ asteroids: [Int]) -> [Int] {
 3     guard asteroids.count > 1 else{
 4         return asteroids
 5     }
 6     
 7     var stack : [Int] = []
 8     
 9     outerLoop: for asteroid in asteroids{
10         while !stack.isEmpty && stack.last! >= 0 && asteroid < 0{
11             if stack.last! + asteroid == 0{
12                 stack.removeLast()
13                 continue outerLoop
14             }else if stack.last! + asteroid > 0 {
15                 continue outerLoop
16             }else{
17                 stack.removeLast()
18             }
19         }
20         stack.append(asteroid)
21         
22     }
23     return stack
24   }
25 }
相關文章
相關標籤/搜索