题目:

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]..

    分析:

    class Solution {public int[] asteroidCollision(int[] as) {//给定一堆数字模拟小行星碰撞(正数代表向右,负数代表向左),元素代表大小。当两个行星碰撞时,小的会爆炸(类似碰碰球)。//如果尺寸大小相同,则都会爆炸。相同方向的永远不会碰撞。返回其碰撞的状态。//思路:利用stack存储,每次对比栈顶元素(相同的话就放入栈中),否则就对比,直至栈顶相同为止//注意:当栈顶为负数时,直接进栈。只有当栈顶为正数才需要发生碰撞Stack<Integer> stack=new Stack<Integer>();for(int i=0;i<as.length;i++){//如果栈为空或者大于0if(stack.isEmpty()||as[i]>0){stack.push(as[i]);continue;}//遇见不同方向元素while(true){int pre=stack.peek();if(pre<0){//当栈顶为负数时stack.push(as[i]);break;}else if(pre==-as[i]){//两个元素相同,消掉栈顶元素stack.pop();break;}else if(pre>-as[i]){//栈顶元素更大,消掉break;}else {//栈顶元素更小,依次迭代和栈顶比较,直至满足条件stack.pop();//如果栈为空了,就直接放入,否则继续循环比较if(stack.isEmpty()){stack.push(as[i]);break;}}}     }//将stack结果输出int [] res=new int[stack.size()];int i=stack.size()-1;while(!stack.empty()){res[i--]=stack.pop();    }return res;}
    }

LeetCode.735 Asteriod Collision相关推荐

  1. LeetCode 735. 行星碰撞(栈)

    1. 题目 给定一个整数数组 asteroids,表示在同一行的行星. 对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动).每一颗行星以相同的速度 ...

  2. Java实现 LeetCode 735 行星碰撞(栈)

    735. 行星碰撞 给定一个整数数组 asteroids,表示在同一行的行星. 对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动).每一颗行星以相 ...

  3. LeetCode-735 Asteriod Collision

    转自https://blog.csdn.net/xiakexiaohu/article/details/78639927 题目: We are given an array asteroids of ...

  4. 2022-4-7 Leetcode 735.行星碰撞

    第一版,没有想到相向移动不会被撞 class Solution {public:vector<int> asteroidCollision(vector<int>& a ...

  5. Leetcode 735. 行星碰撞

    基本思路: 很简单.首先来一个辅助数组,最好是用栈,但是返回的结果不是数组么.所以直接用数组吧.      然后这么考虑: 从左往右遍历输入的数组,每遍历到一个值,若它能和辅助数组的最右边的行星发生碰 ...

  6. leetcode刷题规划

    LeetCode精华题目列表[刷题规划系列] – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advan ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  8. 【LeetCode】735. 行星碰撞

    题目 735. 行星碰撞 难度中等342收藏分享切换为英文接收动态反馈 给定一个整数数组 asteroids,表示在同一行的行星. 对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方 ...

  9. Leetcode刷题记录 735. 行星碰撞

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

最新文章

  1. ddr42400和2660混插_DDR4 2400和3000区别大吗 低频和高频内存性能差距对比
  2. record-09 ATM 过程思想 综合练习
  3. php中mvc代表什么意思,php mvc是什么意思?
  4. 你应该如何正确健壮后端服务?
  5. 深入理解Java中异常体系
  6. 如何完全屏蔽Chrome的提示:请停用以开发者模式运行的扩展程序
  7. LeetCode 801. 使序列递增的最小交换次数(动态规划)
  8. nginx动静分离配置_Nginx 动静分离与负载均衡的实现
  9. matlab 运算程序时间计算
  10. ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.33.10' (111) 解决方法
  11. html按钮点击后无效,关于html中按钮的单击事件,第一次单击可以运行,再次单击不能运行的解决方法...
  12. 第一篇:wine介绍
  13. 嵌入式网络基础知识——MQTT引入
  14. python-英文字母的大小写转换
  15. 快来天津科技大学找我玩
  16. 五种常见的计算机高级语言,[转]计算机语言的种类总结
  17. RegExp (regular expression) object
  18. 我爬取了猪八戒网站的信息,发现了程序员做威客不得不进的坑
  19. 企业实战LNMP-安装wordpress论坛
  20. miniui 和ajax,miniUI的异步请求

热门文章

  1. 使用SAF Spectrum Compact频谱仪进行空闲信道评估
  2. 66道史上最全Redis面试题,面试官能问的都被我找到了(附答案)
  3. 本人实现的视频翻译字幕并导出srt
  4. C语言:【U-boot 和 kernel】添加【调试log宏】
  5. 2022年全国计算机二级考试MS Office题库软件(考试原题)
  6. gige vision协议栈
  7. 【LeetCode刷题】1624. 两个相同字符之间的最长子字符串
  8. Security流程
  9. 使用Gradle构建变体
  10. 测试开发工程师的薪资上限究竟在哪?年薪100W都不是梦...