This commit is contained in:
huangge1199 2021-03-23 08:30:38 +08:00
parent b36adacb88
commit 0a1860cabe
3 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,90 @@
package leetcode.editor.cn;
//给定一个整数数组 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 向右移动 由于移动方向相同的行星不会发生碰撞所以最终没有行星发生碰撞
//
//
//
// 提示
//
//
// 2 <= asteroids.length <= 104
// -1000 <= asteroids[i] <= 1000
// asteroids[i] != 0
//
// Related Topics
// 👍 135 👎 0
import java.util.Stack;
public class AsteroidCollision{
public static void main(String[] args) {
Solution solution = new AsteroidCollision().new Solution();
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] asteroidCollision(int[] asteroids) {
Stack<Integer> stack = new Stack<>();
for (int num : asteroids) {
boolean isIn = true;
while (!stack.isEmpty() && num < 0 && stack.peek() > 0) {
if (num + stack.peek() < 0) {
stack.pop();
} else if (num + stack.peek() > 0) {
isIn = false;
break;
} else {
isIn = false;
stack.pop();
break;
}
}
if (isIn) {
stack.push(num);
}
}
int size = stack.size();
asteroids = new int[size];
for (int i = size - 1; i >= 0; i--) {
asteroids[i] = stack.pop();
}
return asteroids;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long