leetcode 735. Asteroid Collision

3人阅读 评论(0) 收藏 举报
分类:

leetcode 735. Asteroid Collision

题意:给你一个数组,正数会往右移,负数会往左移,移动速度一样,两个数字相撞的话,绝对值大的数会破坏绝对值小的数,问你最后数组的状态。

模拟一下,应该不难。

class Solution {
public:
	vector<int> asteroidCollision(vector<int>& asteroids) {
		stack<int> ans;
		for (int i = 0; i<asteroids.size(); i++)
		{
			if (ans.empty()) ans.push(asteroids[i]);
			else
			{
				if (ans.top() < 0 && asteroids[i] < 0)
					ans.push(asteroids[i]);
				else if (ans.top() > 0 && asteroids[i] > 0)
					ans.push(asteroids[i]);
				else if (ans.top() < 0 && asteroids[i] > 0)
					ans.push(asteroids[i]);
				else if (ans.top() > 0 && asteroids[i] < 0)
                                { 
                                        int flag = 1;
					while (!ans.empty())
					{
						int num = ans.top();
						if (num > 0 && num < abs(asteroids[i]))// 
						{
							ans.pop();
						}
						else if (num > 0 && num > abs(asteroids[i]))
						{
							flag = 0;
							break;
						}
						else if (num > 0 && num == abs(asteroids[i]))
						{
							flag = 0;
							ans.pop();
							break;
						}
                                                else if (num < 0)
						{
							break;
						}
					}
					if (flag == 1) ans.push(asteroids[i]);
				}
			}
		}
		int size = ans.size();
		vector<int> aans(size);
		size--;
		while (!ans.empty())
		{
			aans[size--] = ans.top();
			ans.pop();
		}
		return aans;
	}
};

查看评论

LeetCode 735 Asteroid Collision (栈)

LeetCode 735 Asteroid Collision (栈)
  • Tc_To_Top
  • Tc_To_Top
  • 2017-12-25 13:51:18
  • 281

[Leetcode] 735. Asteroid Collision 解题报告

题目: We are given an array asteroids of integers representing asteroids in a row. For each as...
  • magicbean2
  • magicbean2
  • 2018-03-01 15:52:51
  • 38

LWC 60:735. Asteroid Collision

LWC 60:735. Asteroid Collision传送门:735. Asteroid CollisionProblem: We are given an array asteroids ...
  • u014688145
  • u014688145
  • 2017-11-29 13:03:50
  • 304

735. Asteroid Collision

We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the...
  • zjucor
  • zjucor
  • 2017-11-26 12:11:34
  • 222

LeetCode.735 Asteriod Collision

题目: We are given an array asteroids of integers representing asteroids in a row. For each as...
  • xiakexiaohu
  • xiakexiaohu
  • 2017-11-26 21:11:41
  • 212

leetcode 735. Asteroid Collision 行星碰撞 + 类似括号配对 + 栈

We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the...
  • JackZhang_123
  • JackZhang_123
  • 2017-12-25 16:32:07
  • 54

LWC 60:736. Parse Lisp Expression

LWC 60:736. Parse Lisp Expression传送门:736. Parse Lisp ExpressionProblem: You are given a string exp...
  • u014688145
  • u014688145
  • 2017-11-29 14:44:11
  • 330

Leetcode_735 Asteroid Collision

鉴于最近我可爱的她一直在写博客,突然想起我好久没有写过博客了。虽然我写的博客只想有心人能够看到,但是偶尔写写休息休息还是可以的,今天就拿一道leetcode的题目来写博客吧。 题目: We are...
  • m0_37567543
  • m0_37567543
  • 2017-12-02 01:04:16
  • 71

Asteroid Collision问题及解法

735. Asteroid Collision LeetCode
  • u011809767
  • u011809767
  • 2017-12-13 10:07:24
  • 116

Asteroid Collision

Asteroid Collision
  • sinat_27180563
  • sinat_27180563
  • 2017-11-30 19:55:03
  • 33
    个人资料
    持之以恒
    等级:
    访问量: 8321
    积分: 585
    排名: 9万+
    最新评论