Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
Assume that the total area is never beyond the maximum possible value of int.
[分析]
常规思路:判断两个矩形是否有重叠,无重叠返回面积加和即可,有重叠检查是否是包含关系,包含关系返回大面积,不是包含关系的面积加和减去重叠域面积。
非常规思路:参考https://leetcode.com/discuss/43549/just-another-short-way
public class Solution { // Method 2 public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int left = max(A,E), right = max(min(C,G), left); int bottom = max(B,F), top = max(min(D,H), bottom); return (C-A)*(D-B) - (right-left)*(top-bottom) + (G-E)*(H-F); } // Method 1 public int computeArea1(int A, int B, int C, int D, int E, int F, int G, int H) { int area1 = (C - A) * (D - B); int area2 = (G - E) * (H - F); if (G <= A || C <= E || D <= F || H <= B) return area1 + area2; if ((A <= E && G <= C) && (B <= F && H <= D)) return area1; if ((E <= A && C <= G) && (F <= B && D <= H)) return area2; int xOverlap = getOverlap(A, C, E, G); int yOverlap = getOverlap(B, D, F, H); return area1 + area2 - xOverlap * yOverlap; } public int getOverlap(int a1, int a2, int b1, int b2) { if (a1 <= b1 && b2 <= a2) return b2 - b1; if (b1 <= a1 && a2 <= b2) return a2 - a1; if (a1 <= b1 && b1 <= a2) return a2 - b1; if (a1 <= b2 && b2 <= a2) return b2 - a1; return 0; } }
相关推荐
《leetcode-solutions》,刷算法题,需要有一定的英文阅读能力。。。
Algorithm-LeetCode-Sol-Res.zip,干净,易懂的解决方案和资源,为leetcode在线判断算法问题。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
IDEA 插件,lettcode刷题,leetcode-editor7.4版本下载进行本地导入(直接将压缩包拖进IDEA即可)
Algorithm-leetcode-spider.zip,leetcode公司,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
在IDE中解决LeetCode问题,支持leetcode.com与leetcode-cn.com,满足基本的做题需求。 理论上支持: IntelliJ IDEA PhpStorm WebStorm PyCharm RubyMine AppCode CLion GoLand DataGrip Rider MPS Android Studio。
leetcode-cli-plugins leetcode-cli 的第 3 方插件。 什么是 如何使用 如何使用 插件 名称 描述 增强的命令 按公司或标签过滤问题 list 不要在同一台计算机上使 Chrome 的会话过期 login 不要在同一台计算机上使 ...
解题思路思路和LeetCode-python 503.下一个更大元素 II一致,只是这里求的是下标的距离,而不是数值倒序搜索,用到栈,栈里存储索引情况1:若栈为
leetcode-cheat 的发布 它是什么 ? 这是一个chrome 扩展,可以帮助您更高效地使用 leetcode。您可以从 重要: leetcode-cheat 现在只支持中文版。 也就是说不完全支持leetcode.com,但是你可以用leetcode-cn.com代替...
leetcode 答案解析 golang解答
leetcode-editor,在ide中做leetcode练习,支持leetcode.com和leetcode-cn.com,以满足练习的基本需求。理论上支持:intellij idea phpstorm webstorm pycharm rubymine appcode clion goland datagrip rider mps ...
~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/vsc-leetcode-cli/bin/leetcode /usr/local/bin/leetcode 修改模板 open ~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/...
leetcode-helper-1.7.1
970. 强整数对数运算function powerfulIntegers(x: number, y: number, bound: number): numb
然后进入到LeetCode-Spider目录中修改config.json,其中outputDir需要填写该工程的/docs/views文件夹路径 { "username": "aaa", "password": "bbb", "outputDir": "/Users/liuyao/Downloads/LeetCode-Blog-Test/docs...
leetcode-tag-dynamic programming
leetcode-cli 注意:这个存储库是为了临时使用而分叉的。 注意:从 webbrowser 复制 cookie 并使用leetcode user -c可以临时修复不能。 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的...
leetcode-tag-Tree
leetcode-tag-Stack
leetcode-tag-array