Perfect Rectangle

http://www.cnblogs.com/grandyang/p/5825619.html

class Solution {
public:
	bool isRectangleCover(vector<vector<int>>& rectangles) {
		unordered_set<string> st;
		int min_x = INT_MAX, min_y = INT_MAX, max_x = INT_MIN, max_y = INT_MIN, area = 0;
		for (auto rect : rectangles) {
			min_x = min(min_x, rect[0]);
			min_y = min(min_y, rect[1]);
			max_x = max(max_x, rect[2]);
			max_y = max(max_y, rect[3]);
			area += (rect[2] - rect[0]) * (rect[3] - rect[1]);
			string s1 = to_string(rect[0]) + "_" + to_string(rect[1]); // bottom-left
			string s2 = to_string(rect[0]) + "_" + to_string(rect[3]); // top-left
			string s3 = to_string(rect[2]) + "_" + to_string(rect[3]); // top-right
			string s4 = to_string(rect[2]) + "_" + to_string(rect[1]); // bottom-right
			if (st.count(s1)) st.erase(s1);
			else st.insert(s1);
			if (st.count(s2)) st.erase(s2);
			else st.insert(s2);
			if (st.count(s3)) st.erase(s3);
			else st.insert(s3);
			if (st.count(s4)) st.erase(s4);
			else st.insert(s4);
		}
		string t1 = to_string(min_x) + "_" + to_string(min_y);
		string t2 = to_string(min_x) + "_" + to_string(max_y);
		string t3 = to_string(max_x) + "_" + to_string(max_y);
		string t4 = to_string(max_x) + "_" + to_string(min_y);
		if (!st.count(t1) || !st.count(t2) || !st.count(t3) || !st.count(t4) || st.size() != 4) return false;
		return area == (max_x - min_x) * (max_y - min_y);
	}
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值