leetcode 735. Asteroid Collision

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Sea_muxixi/article/details/79957786

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;
	}
};

阅读更多

Collision

07-22

Problem DescriptionnMatt is playing a naive computer game with his deeply loved pure girl.nnThe playground is a rectangle with walls around. Two balls are put in different positions inside the rectangle. The balls are so tiny that their volume can be ignored. Initially, two balls will move with velocity (1, 1). When a ball collides with any side of the rectangle, it will rebound without loss of energy. The rebound follows the law of refiection (i.e. the angle at which the ball is incident on the wall equals the angle at which it is reflected).nnAfter they choose the initial position, Matt wants you to tell him where will the two balls collide for the first time.n nnInputnThe first line contains only one integer T which indicates the number of test cases.nnFor each test case, the first line contains two integers x and y. The four vertices of the rectangle are (0, 0), (x, 0), (0, y) and (x, y). (1 ≤ x, y ≤ 105)nnThe next line contains four integers x1, y1, x2, y2. The initial position of the two balls is (x1, y1) and (x2, y2). (0 ≤ x1, x2 ≤ x; 0 ≤ y1, y2 ≤ y)n nnOutputnFor each test case, output “Case #x:” in the first line, where x is the case number (starting from 1). nnIn the second line, output “Collision will not happen.” (without quotes) if the collision will never happen. Otherwise, output two real numbers xc and yc, rounded to one decimal place, which indicate the position where the two balls will first collide.n nnSample Inputn3n10 10n1 1 9 9n10 10n0 5 5 10n10 10n1 0 1 10n nnSample OutputnCase #1:n6.0 6.0nCase #2:nCollision will not happen.nCase #3:n6.0 5.0n

没有更多推荐了,返回首页