博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode735. 行星碰撞 | Asteroid Collision
阅读量:5311 次
发布时间:2019-06-14

本文共 8918 字,大约阅读时间需要 29 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝()
➤GitHub地址:
➤原文地址:  
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

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.

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:

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

Example 3:

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:

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:

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

给定一个整数数组 asteroids,表示在同一行的行星。

对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动)。每一颗行星以相同的速度移动。

找出碰撞后剩下的所有行星。碰撞规则:两个行星相互碰撞,较小的行星会爆炸。如果两颗行星大小相同,则两颗行星都会爆炸。两颗移动方向相同的行星,永远不会发生碰撞。

示例 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             else14             {15               isAddNegative = true16               while positiveItems.count > 017               {18                   19                 if positiveItems.last! > abs(asteroid)20                 {21                     isAddNegative = false22                     break23                 }24                 else 25                 {26                     let lastPositiveItem = positiveItems.removeLast()27 28                     if lastPositiveItem == abs(asteroid)29                     {30                         isAddNegative = false31                         break32 33                     }                    34                   }35                 }36                 37                 if isAddNegative38                 {39                      negativeItems.append(asteroid)40                 }41             }            42         }43         44         return negativeItems + positiveItems45         46     }47 }

Runtime: 88 ms
Memory Usage: 19.2 MB
1 class Solution { 2     struct Stack
{ 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.isEmpty14 }15 16 func peek() -> T? {17 return self.temperatures.last18 }19 20 func contents() -> [T] {21 return self.temperatures22 }23 }24 25 func asteroidCollision(_ asteroids: [Int]) -> [Int] {26 27 var stack:Stack = Stack
()28 29 for asteroid in asteroids {30 var isShouldPush = true31 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 : false37 }else{38 isShouldPush = false39 }40 41 if isShouldPush == false{42 break43 }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..
0 { 8 output.append(rock) 9 continue10 }11 checkExplosion(&output, rock)12 }13 return output14 }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 // Explode23 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 = true11                 12                 while positives.count > 0 {13                     if positives.last! > -asteroid {14                         shouldAppendToNegative = false15                         break16                     } else {17                         let last = positives.removeLast()18                         19                         if -asteroid == last {20                             shouldAppendToNegative = false21                             break22                         }23                     }24                 }25                 26                 if shouldAppendToNegative {27                     negatives.append(asteroid)28                 }29             }30         }31         32         return negatives + positives33     }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 = true14                         } else if prev.magnitude < ast.magnitude {15                             goR.removeLast()16                         } else {17                             astExploded = true18                         }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 result35     }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 outerLoop14             }else if stack.last! + asteroid > 0 {15                 continue outerLoop16             }else{17                 stack.removeLast()18             }19         }20         stack.append(asteroid)21         22     }23     return stack24   }25 }

 

 

转载于:https://www.cnblogs.com/strengthen/p/10519586.html

你可能感兴趣的文章
JS写一个简单日历
查看>>
Python 发 邮件
查看>>
mysql忘记密码的解决办法
查看>>
全面分析Java的垃圾回收机制2
查看>>
[Code Festival 2017 qual A] C: Palindromic Matrix
查看>>
修改博客园css样式
查看>>
Python3 高阶函数
查看>>
初始面向对象
查看>>
docker一键安装
查看>>
leetcode Letter Combinations of a Phone Number
查看>>
Unity 5.4 测试版本新特性---因吹丝停
查看>>
7.5 文件操作
查看>>
DFS-hdu-2821-Pusher
查看>>
MyEclipse中将普通Java项目convert(转化)为Maven项目
查看>>
node js 安装.node-gyp/8.9.4 权限 无法访问
查看>>
windows基本命令
查看>>
VMware中CentOS设置静态IP
查看>>
[poj1006]Biorhythms
查看>>
Hyper-V虚拟机上安装一个图形界面的Linux系统
查看>>
Hover功能
查看>>