完美矩形(C++)
2021/11/16 22:09:52
本文主要是介绍完美矩形(C++),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
typedef pair<int, int> Point;
class Solution {
public:
bool isRectangleCover(vector<vector<int>>& rectangles) {
long area = 0;
int minX = rectangles[0][0], minY = rectangles[0][1], maxX = rectangles[0][2], maxY = rectangles[0][3];
map<Point, int> cnt;
for (auto & rect : rectangles) {
int x = rect[0], y = rect[1], a = rect[2], b = rect[3];
area += (long) (a - x) * (b - y);
minX = min(minX, x);
minY = min(minY, y);
maxX = max(maxX, a);
maxY = max(maxY, b);
Point point1({x, y});
Point point2({x, b});
Point point3({a, y});
Point point4({a, b});
cnt[point1] += 1;
cnt[point2] += 1;
cnt[point3] += 1;
cnt[point4] += 1;
}
Point pointMinMin({minX, minY});
Point pointMinMax({minX, maxY});
Point pointMaxMin({maxX, minY});
Point pointMaxMax({maxX, maxY});
if (area != (long long) (maxX - minX) * (maxY - minY) || !cnt.count(pointMinMin) || !cnt.count(pointMinMax) || !cnt.count(pointMaxMin) || !cnt.count(pointMaxMax)) {
return false;
}
cnt.erase(pointMinMin);
cnt.erase(pointMinMax);
cnt.erase(pointMaxMin);
cnt.erase(pointMaxMax);
for (auto & entry : cnt) {
int value = entry.second;
if (value != 2 && value != 4) {
return false;
}
}
return true;
}
};
这篇关于完美矩形(C++)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南
- 2025-01-03图像文字理解,OCR、大模型还是多模态模型?PalliGema2在QLoRA技术上的微调与应用
- 2025-01-03混合搜索:用LanceDB实现语义和关键词结合的搜索技术(应用于实际项目)
- 2025-01-03停止思考数据管道,开始构建数据平台:介绍Analytics Engineering Framework
- 2025-01-03如果 Azure-Samples/aks-store-demo 使用了 Score 会怎样?
- 2025-01-03Apache Flink概述:实时数据处理的利器
- 2025-01-01使用 SVN合并操作时,怎么解决冲突的情况?-icode9专业技术文章分享
- 2025-01-01告别Anaconda?试试这些替代品吧
- 2024-12-31自学记录鸿蒙API 13:实现人脸比对Core Vision Face Comparator
- 2024-12-31自学记录鸿蒙 API 13:骨骼点检测应用Core Vision Skeleton Detection