Leetcode:223. Rectangle Area(JAVA)
来源:互联网 发布:香港地下判官 知乎 编辑:程序博客网 时间:2024/04/28 08:17
【问题描述】
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.
【思路】
先计算两个矩形面积,再判断是否需要减去中间重叠部分面积。
中间矩形(leftx, lefty) (rightx, righty)
int leftx = Math.max(A, E);
int lefty = Math.max(B, F);
int rightx = Math.min(C, G);
int righty = Math.min(D, H);
【code】
public class Solution { public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {int leftx = Math.max(A, E);int lefty = Math.max(B, F);int rightx = Math.min(C, G);int righty = Math.min(D, H);int area = (C - A) * (D - B) + (G - E) * (H - F);if (leftx >= rightx || lefty >= righty) {return area;}return area - (rightx - leftx) * (righty - lefty); }}
0 0
- Leetcode:223. Rectangle Area(JAVA)
- 【leetcode】Rectangle Area【java】
- [leetcode] 223.Rectangle Area
- [leetcode] 223. Rectangle Area
- 223. Rectangle Area LeetCode
- #leetcode# 223. Rectangle Area
- leetcode 223. Rectangle Area
- LeetCode--223. Rectangle Area
- LeetCode *** 223. Rectangle Area
- LeetCode 223. Rectangle Area
- leetcode-223. Rectangle Area
- LeetCode-223.Rectangle Area
- 223.[Leetcode]Rectangle Area
- leetcode 223. Rectangle Area
- 【leetcode】223. Rectangle Area
- [Leetcode]223. Rectangle Area
- LeetCode - 223. Rectangle Area
- LeetCode 223. Rectangle Area
- 岭回归
- oracle实现mysql的if exist drop table
- (一)创建一个新的安卓工程
- 【JAVA】38、构造方法的定义
- 导入Oracle的dmp文件
- Leetcode:223. Rectangle Area(JAVA)
- RESTful之Content negotiation
- 桶排序
- Youth by Samuel Ullman
- 蓝桥杯 牌型种数 (暴力||dfs)
- 内存水线计算
- 三层总结
- 数据库排序查询(一个表里面先分类再按照某一项的最大值)
- 最短路径之Dijkstra算法