Leetcode刷题记录 735. 行星碰撞
给定一个整数数组 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 。
class Solution {public:vector<int> asteroidCollision(vector<int>& asteroids) {stack<int> s; //用于保存不碰撞的元素s.push(asteroids[0]);for(int i=1;i<asteroids.size();i++){//一直要注意这几个边界条件if(s.empty() || (asteroids[i]>0 && s.top()>0) || s.top()<0){s.push(asteroids[i]);}else{int x=asteroids[i];/*自己做的时候一直会报一个空指针的错误,原因是此处要把s.empty()放在第一个,不然在判断s.top()的时候就会报空指针的错误*/while(!s.empty() && x<0 && s.top()>0){if(abs(x)<s.top()){x=s.top();s.pop();break;}else if(abs(x)==s.top()){x=0; //0代表后续两个都不会进栈s.pop();break;}else{s.pop();}}if(x!=0){s.push(x);}}}int n=s.size();vector<int> result(n);for(int i=n-1;i>=0;i--){result[i]=s.top();s.pop();}return result;}
};
Leetcode刷题记录 735. 行星碰撞相关推荐
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- LeetCode刷题记录12——232. Implement Queue using Stacks(easy)
LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...
- LeetCode刷题记录11——290. Word Pattern(easy)
LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...
- LeetCode刷题记录10——434. Number of Segments in a String(easy)
LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...
- LeetCode刷题记录9——58. Length of Last Word(easy)
LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...
- LeetCode刷题记录8——605. Can Place Flowers(easy)
LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...
- LeetCode刷题记录7——824. Goat Latin(easy)
LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...
最新文章
- 每周一个 Python 模块 | time
- 面向对象八大设计原则
- 数据增强,将数据增强后保存到本地
- Android5.0源码分析—— Zygote进程分析
- 查看oracle数据库的连接数以及用户 (转:http://blog.itpub.net/24558279/viewspace-752293/)
- mongodb 主键_MongoDB主键是您的朋友
- html频谱跳动效果,HTML5音频可视化频谱跳动代码
- 【数值分析】数值分析的微积分学基础
- API和Web API(1)
- centos7 防火墙_【Linux简单实用小命令001】CentOS 7、8的防火墙端口开放
- How to deploy Odoo 11 on Ubuntu 18.04
- 由于项目需要 接触使用特别好 亲测使用 GoEasy小程序即时通讯源码是一个基于GoEasy提供的websocket通讯服务,实现的小程序即时通讯,
- NotifyIcon控件的使用
- perl中uc,lc,ucfirst,lcfirst的用法(转载)
- html embed如何禁止自动播放,html embed用法
- win10重启不能上网(只能QQ上网,打不开浏览器网页)
- nodejs 使用jsonwebtoken进行权限验证
- 20190707 - FE:使用 Charles 在 IphoneX 上抓包
- Ubuntu支持LinuxONE大型机:为云而生的强强新组合
- 怎么防止过曝_在拍摄的时候,要如何避免闪光灯过曝?