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处理,这类问题较简单,求解时间复杂度一 ...
 
随机推荐
- AC日记——魔术球问题 洛谷 P2765
			
题目描述 «问题描述: 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全 ...
 - null相关
			
本文转自:http://www.cnblogs.com/peida/archive/2013/06/14/Guava_Optional.html null代表不确定的对象: Java中,null是一个 ...
 - BZOJ1005明明的烦恼 Prufer + 分解質因數 + 高精度
			
@[高精度, Prufer, 質因數分解] Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在 任意两点间连线,可产生多 ...
 - 数据结构------------------二叉查找树(BST)的java实现
			
数据结构------------------二叉查找树(BST)的java实现 二叉查找树(BST)是一种能够将链表插入的灵活性和有序数组查找的高效性相结合的一种数据结构.它的定义如下: 二叉查找树是 ...
 - ios7.1后setting中没有开启相机服务应用程序相机预览黑屏问题
			
if ( [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0){ ...
 - 邁向IT專家成功之路的三十則鐵律 鐵律二:IT專家專業之道–專精
			
在IT技術的領域當中有許多的類別,若要細分那可真是難以一一列舉,但常見的大致有軟體研發工程師.韌體研發工程師.系統分析師.網路工程師.系統工程師.維護工程師.動畫設計師.製圖工程師.以及各類別的專業電 ...
 - Android 关于view的getLayoutParams().width,getWidth(),getMeasuredWidth();
			
习惯了使用xml的布局方式,当动态布局的时候就有许多疑点,记录一下,帮助我这老头一样的记忆力. 网上也有许多解析这getLayoutParams().width,getWidth(),getMeasu ...
 - 渗透测试思路 | Linux下自动化搭建FakeAP,劫持用户在Portal认证下的所有流量
			
如何在linux下搭建一个fakeap,使得portal认证下的用户无法发现连接你的假AP,并且能够正常上网.先说一下portal认证.无线WIFI认证方式主要有wpa2 和 open两种,而port ...
 - Utuntu 和 window共享文件
			
由于自己想用服务器跑代码,数据集和模型一般都在本机电脑上,用实验室服务器需要拷贝数据或者,在服务器上重新下载数据很麻烦 都在局域网内可以实现文件共享,代码和数据都在本地,共享给服务器,只需要使用服务器 ...
 - [c++菜鸟]《Accelerate C++》读书笔记
			
第0章 开始学习C++ 1.<<的行为取决于它的操作数类型,<<会把它的右操作数的字符写到左操作数所指示的流中,他是结果就是它的左操作数. 2.std::endl是一个控制器, ...