[LeetCode] 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 most10000
. - Each asteroid will be a non-zero integer in the range
[-1000, 1000].
.
这道题用一个数组来模拟行星碰撞,正数代表行星向右移动,负数表示向左移动,绝对值大小表示行星的质量,如果两个相邻的行星相向移动会碰撞,质量大的行星会完好无损的保存,质量小的就会灰飞烟灭。那么博主最开始想的方法就是按照题目要求来一个一个的处理,我们先把给定的数组放到结果res中,然后进行while循环,如果此时结果res中的数字个数小于等于1个,直接返回即可,没有可碰撞的了。否则我们建立一个临时数组t,把结果res中的首元素放到t中,然后从第二个数字开始遍历结果res,如果此时t为空了,或者当前数字大于0而t数组最后一个数字小于0(此时两个行星向相反方向运动,不会相撞),或者两个数字的符号相同(此时两个行星向同一个方向运动,不会相撞),这三种情况下都把当前数字res[i]加到数组t中;那么剩下的情况就是两个行星相向运动了,如果两个数字相加等于0,则说明两个行星质量相同,且相向运动,则一起消失,我们将数组t中最后一个数字移除;如果当前数字小于0,且两个数字相加小于0,那么此时相撞后会留下质量大的行星,我们将数组t的最后一个数字赋值为res[i]即可。for循环之和,如果数组t和结果res的大小相等,说明此时状态已经稳定了,我们直接break,否则就把数组t赋值给结果res并继续循环,参见代码如下:
解法一:
class Solution {
public:
vector<int> asteroidCollision(vector<int>& asteroids) {
vector<int> res = asteroids;
while (true) {
if (res.size() <= ) return res;
vector<int> t{res[]};
for (int i = ; i < res.size(); ++i) {
if (t.empty() || (res[i] > && t.back() < ) || res[i] * t.back() > ) {
t.push_back(res[i]);
} else if (res[i] + t.back() == ) {
t.pop_back();
} else if (res[i] < && res[i] + t.back() < ) {
t.back() = res[i];
}
}
if (t.size() == res.size()) break;
else res = t;
}
return res;
}
};
实际上我们可以写的更加简洁一些,我们遍历所有的数字,如果当前数字是正数的话,我们直接加入结果res;否则我们遇到的都是负数,如果结果res为空,或者结果res的最后一个数字小于0(此时两个行星同时向左运动),直接将当前数字加入结果res;如果结果res的最后一个数字(此时为正数)小于当前数字的绝对值,说明碰撞后消失了,那么我们将i自减一个,然后将res最后一个数字移除,这样下次遍历的时候还是这个质量大的行星。如果两个质量相等,那么直接移除res最后一个数字,此时两个行星都消失了,参见代码如下:
解法二:
class Solution {
public:
vector<int> asteroidCollision(vector<int>& asteroids) {
vector<int> res;
for (int i = ; i < asteroids.size(); ++i) {
if (asteroids[i] > ) {
res.push_back(asteroids[i]);
} else if (res.empty() || res.back() < ) {
res.push_back(asteroids[i]);
} else if (res.back() <= -asteroids[i]) {
if (res.back() < -asteroids[i]) --i;
res.pop_back();
}
}
return res;
}
};
类似题目:
参考资料:
https://discuss.leetcode.com/topic/112034/java-c-clean-code
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Asteroid Collision 行星碰撞的更多相关文章
- LeetCode——735.行星碰撞
给定一个整数数组 asteroids,表示在同一行的行星. 对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动).每一颗行星以相同的速度移动. 找出 ...
- [LeetCode] 735. Asteroid Collision
行星碰撞. 题意是给一个数组 asteroids,表示在同一行的行星.对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动).每一颗行星以相同的速度移 ...
- Java实现 LeetCode 735 行星碰撞(栈)
735. 行星碰撞 给定一个整数数组 asteroids,表示在同一行的行星. 对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动).每一颗行星以相 ...
- [Swift]LeetCode735. 行星碰撞 | Asteroid Collision
We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...
- 【LeetCode】735. Asteroid Collision 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 小行星碰撞 Asteroid Collision
2018-08-07 11:12:01 问题描述: 问题求解: 使用一个链表模拟栈,最后的状态一定是左侧全部是负值,表示的是向左飞行,右侧的全部是正值,表示的是向右飞行. 遍历整个数组,对于每个读到的 ...
- 【狼】unity3d collision获取碰撞的点的位置
void OnCollisionEnter(Collision ctl) { ContactPoint contact = ctl.contacts[]; Quaternion rot = Quate ...
- 735. Asteroid Collision彗星相撞后的消失数组
[抄题]: We are given an array asteroids of integers representing asteroids in a row. For each asteroid ...
- Phaser Matter Collision Plugin 碰撞插件 -- iFiero技术分享
collision-simple-demo Phaser 自带的Arcade虽然易用,但复杂的物理碰撞明显就不够用了,于是Matter等物理引擎还是不得不学的,以下是Matter物理体碰撞的一个插件, ...
随机推荐
- Oracle 琐表和查询谁在琐表并解决
Oracle数据库操作中,我们有时会用到锁表查询以及解锁和kill进程等操作,那么这些操作是怎么实现的呢?本文我们主要就介绍一下这部分内容. (1)锁表查询的代码有以下的形式: select coun ...
- QQ数据库管理
1,数据库关系图 ##用例1:查询数据 #01.查询QQ号码为54789625的所有好友信息,包括QQ号码,昵称,年龄 select RelationQQID as QQ号码,NickName as ...
- python爬虫---抓取优酷的电影
最近在学习爬虫,用的BeautifulSoup4这个库,设想是把优酷上面的电影的名字及链接爬到,然后存到一个文本文档中.比较简单的需求,第一次写爬虫.贴上代码供参考: # coding:utf-8 i ...
- python全栈学习--day9(函数初始)
Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...
- Eclipse+Pydev环境搭建
1,准备好Eclipse和JAVA,x64 2,安装JDK,配置JAVA环境变量,假设安装路径为 C:\Program Files\Java\jdk1.8.0_161 在系统变量中,新建CLASSPA ...
- 第2次作业:Wechat创作史
Wechat创作史 比尔盖茨曾经说过一句话:21世纪要么电子商务,要么无商可务. 2.1 介绍产品相关信息 -the information about Wechat 你选择的产品是? 选择微信作 ...
- C语言--函数嵌套
一.实验作业 注意: 1.可以先初始化2个结构体数组数据以便测试. 2.要求用模块化方式组织程序结构,合理设计各自定义函数.同时,程序能够进行异常处理,检查用户输入数据的有效性,用户输入数据有错误,如 ...
- Week02-Java基本语法与类库
1. 本周学习总结 本周主要学习了包装类,明白了包装类比基本数据类型的优点(见书面作业4.2),了解了自动装箱,自动拆箱的概念和区别(见书面作业4.1),知道了java中的引用类似C语言中的指针,明白 ...
- jQuery函数学习
函数:after(content) 功能:在每个匹配的元素后面添加html内容 返回:jQuery对象 参数:content (<Content>): Content to insert ...
- css变化代码
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...