leeetcode 735. Asteroid Collision
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]..
一开始没有认真思考,直接用一个队列去存储正数,然后遇到负数就进行合并判断。时间复杂度接近O(n2)但一定小于n2.下面是代码。其实可以用s代替ans见第二分代码.
class Solution {
public:
vector<int> asteroidCollision(vector<int>& asteroids) {
vector<int> s;
vector<int> ans;
for (auto x : asteroids) {
if (x > 0) s.emplace_back(x);
else {
int mark = 0;
while (!s.empty()) {
int u = s.back();
if (-x > u) s.pop_back();
else if (-x == u) {
mark = 1;
s.pop_back(); break;
} else {
break;
}
}
if (s.empty() && !mark) ans.push_back(x);
}
}
for (auto x : s) {
ans.push_back(x);
}
return ans;
}
};
用一个s就可以解决,如果s中有负数那么就不用在往前找了(前面不可能有正数了).
优化后的代码
class Solution {
public:
vector<int> asteroidCollision(vector<int>& asteroids) {
vector<int> s;
for (auto x : asteroids) {
if (x > 0) s.emplace_back(x);
else if (x < 0) {
int y = -x;
int mark = 1;
while (!s.empty() && s.back() > 0) {
if (s.back() >= y) {
if (s.back() == y) s.pop_back();
mark = 0;
break;
} else {
s.pop_back();
}
}
if (mark) s.emplace_back(x);
}
}
return s;
}
};
leeetcode 735. Asteroid Collision的更多相关文章
- 735. Asteroid Collision彗星相撞后的消失数组
[抄题]: We are given an array asteroids of integers representing asteroids in a row. For each asteroid ...
- 【LeetCode】735. Asteroid Collision 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- [LeetCode] 735. Asteroid Collision
行星碰撞. 题意是给一个数组 asteroids,表示在同一行的行星.对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动).每一颗行星以相同的速度移 ...
- [LeetCode] Asteroid Collision 行星碰撞
We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...
- [Swift]LeetCode735. 行星碰撞 | Asteroid Collision
We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...
- Asteroid Collision
We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...
- 小行星碰撞 Asteroid Collision
2018-08-07 11:12:01 问题描述: 问题求解: 使用一个链表模拟栈,最后的状态一定是左侧全部是负值,表示的是向左飞行,右侧的全部是正值,表示的是向右飞行. 遍历整个数组,对于每个读到的 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- 算法与数据结构基础 - 堆栈(Stack)
堆栈基础 堆栈(stack)具有“后进先出”的特性,利用这个特性我们可以用堆栈来解决这样一类问题:后续的输入会影响到前面的阶段性结果.线性地遍历输入并用stack处理,这类问题较简单,求解时间复杂度一 ...
随机推荐
- ‘cnpm' 不是内部或外部命令,也不是可运行的程序
昨天用npm 安装环境,实在太慢了,就想用cnpm,然后发现提示‘cnpm' 不是内部或外部命令,也不是可运行的程序. 看了很多方法,选择了下面这个,运气好到爆棚,就直接可以用了.其他的方法暂未去了 ...
- RabbitMQ 延迟队列,消息延迟推送
目录 应用场景 消息延迟推送的实现 测试结果 应用场景 目前常见的应用软件都有消息的延迟推送的影子,应用也极为广泛,例如: 淘宝七天自动确认收货.在我们签收商品后,物流系统会在七天后延时发送一个消息给 ...
- Html中的 http-equiv="X-UA-Compatible" 解释
1.X-UA-Compatible X-UA-Compatible是自从IE8新加的一个设置,对于IE8以下的浏览器是不识别的. 通过在meta中设置X-UA-Compatible的值,可以指定网页的 ...
- rocketMQ--搭建demo的坑
果然不出所料,搭建起来就有坑 ,客户端经典的connection 11911的错误 在我的环境上解决方法加一行配置 brokerClusterName = DefaultClusterbrokerNa ...
- Java获取指定时间(转)
说明:从LocalDate的API上看,主要用于快速获取当前年月日,而DateFormatter也基本上伴随着使用.如果是操作Date对象的,主要是用于时间戳等,伴随着使用的是SimpleDateFo ...
- Android View源码解读:浅谈DecorView与ViewRootImpl
前言 对于Android开发者来说,View无疑是开发中经常接触的,包括它的事件分发机制.测量.布局.绘制流程等,如果要自定义一个View,那么应该对以上流程有所了解.研究.本系列文章将会为大家带来V ...
- Oracle Apex 有用笔记系列 6 - 可编辑交互报告 Editable Interactive Report
据笔者所知.Apex 4.x 是没有提供可编辑交互报告组件的.这就须要我们手动实现. 事实上这也并非非常复杂,仅仅须要简单几步. 1. 依据向导建立一个interactive report.查询语句能 ...
- Linux安装Java/Maven
所需文件:jdk 下载 安装Java INSTALL_PATH=/opt/soft TAR_FILE=/mnt/d/resources/soft/jdk-8u152-linux-x64.tar.gz ...
- impdp因致命错误终止 ORA-7445 [kpodpals]
基本要素 前天好不easy成功给用户把数据全库导出,今天用户又告知导出的数据无法导入,首先就问用户有什么错误提示,给我的回答是就一个'作业"SYSTEM"."SYS_IM ...
- C#.NET开源项目、机器学习、Power BI (转载)
.NET技术, 开源项目, 数据挖掘, 机器学习, 微软Power BI, 足球赛事分析, Matlab与C#编程 博客园 管理 本站首页 头条推荐 Power BI .NET开源 机器学习 博客美化 ...