作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/asteroid-collision/description/

题目描述

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

题目大意

在同一轨道上有一堆小行星,列表给出的是他们的体积。数字的正负代表了他们的移动方向,同样方向的不会相撞,相同方向的会相撞。当相撞时,体积大小相等的两个都会消失,否则就是体积小的小时。求稳定之后留下来的行星。

解题方法

当我们意识到,行星是两两之间互相作用的,那我们很容易就想到了用栈。因为栈能处理这样抵消和遗留的问题。

算法的思想是,从左到右遍历每个行星,并和栈顶数字相比较,当栈顶数字为正(向右),当前数字为负(向左)的时候,会发生碰撞。这时候,判断遗留下来的数字是多少,保存到ast里,如果ast为空代表啥都没有了,如果ast质量大于栈顶元素会留下来ast,否则留下pre。判断ast是否为空,不为空就把遗留下来的数字进栈就好了。

自己犯下的一个严重错误:12行写成了ast == None,检查了n多遍都没检查出来错误!所以刷题写代码一定要一气呵成,不要分心啊!

代码如下:

class Solution(object):
def asteroidCollision(self, asteroids):
"""
:type asteroids: List[int]
:rtype: List[int]
"""
stack = []
for ast in asteroids:
while stack and ast < 0 and stack[-1] >= 0:
pre = stack.pop()
if ast == -pre:
ast = None
break
elif -ast < pre:
ast = pre
if ast != None:
stack.append(ast)
return stack

使用C++写的代码如下,使用了一个bool型变量,表示现在的行星需不需要入栈。默认情况下是需要入栈的,但是当行星质量小于等于栈顶元素的时候,自己就消失了,也就不用入栈了。C++的stack转成vector不太方便,所以我直接使用vector了。

代码如下:

class Solution {
public:
vector<int> asteroidCollision(vector<int>& asteroids) {
vector<int> s;
for (int a : asteroids) {
bool ispush = true;
while (!s.empty() && a < 0 && s.back() > 0) {
int t = s.back();
if (abs(a) > abs(t)) {
s.pop_back();
} else if (abs(a) == abs(t)) {
s.pop_back();
ispush = false;
break;
} else {
ispush = false;
break;
}
}
if (ispush)
s.push_back(a);
}
return s;
}
};

日期

2018 年 7 月 17 日 —— 连天大雨,这种情况很少见,但是很舒服
2018 年 12 月 26 日 —— 转眼就周三了,一万年太久,只争朝夕

【LeetCode】735. Asteroid Collision 解题报告(Python & C++)的更多相关文章

  1. [LeetCode] 735. Asteroid Collision

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

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. 日常Java 2021/10/28

    Java lterator Java lterator(迭代器)不是一个集合,它是一种用于访问集合的方法,可用于迭代 ArrayList和HashSet等集合.lterator是Java迭代器最简单的 ...

  2. 2019广东工业大学新生杯决赛 I-迷途的怪物

    题目:I-I-迷途的怪物_2019年广东工业大学腾讯杯新生程序设计竞赛(同步赛) (nowcoder.com) 将(p-1)^n 按照多项式定理拆开,会发现只有一项没有p,其余项都有p,可直接约掉. ...

  3. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍

    其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...

  4. 【swift】用Xib实现自定义警告框(Alert)(安卓叫法:Dialog对话框)

    在写这篇博客前,先感谢两篇博客 [如何自定义的思路]:https://www.cnblogs.com/apprendre-10-28/p/10507794.html [如何绑定Xib并且使用]:htt ...

  5. linux环境下安装jdk,tomcat

    一.安装tomcat 1.使用docker安装(你得linux服务器上已经安装了docker) 1)执行命令: docker search tomcat; 2)选择第一个镜像进行下载,执行命令:doc ...

  6. 【Linux】【Shell】【Basic】数组

    1. 数组:         变量:存储单个元素的内存空间:         数组:存储多个元素的连续的内存空间:             数组名:整个数组只有一个名字:             数组 ...

  7. 【力扣】146. LRU缓存机制

    运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果关键字 (key) ...

  8. 如何推翻JAVA的统治地位

    "java越来越过份了."php狠狠的说,他转头看着C:"C哥,您可是前辈,java最近砸了我不少场子,你老再不出来管管,我怕他眼里就没有您了啊." C哥吸烟, ...

  9. mysql--求中位数

    第一种求中位数方法: /* 第一步:添加一个正序和反序 第二步:当列表数目为奇数的时候,列表选出的情况,当列表为偶数的时候列表的情况 第三步:统筹奇数和偶数时中位数 */ select sum(Mat ...

  10. 替DateDif哭诉一把(Excel函数集团)

    Excel中有个工作表函数DateDif,专门用来计算两日期之间的日差.月差.年差,传说十分好用. 具体用法在此就省略了,好奇的童鞋请自行*度~ 可是,在Excel里,他却是个"没户口&qu ...