Rectangle Area——LeetCode
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.
题目大意:给定两个矩形的左下、右上坐标,求出两个矩形一共覆盖的面积。
解题思路:两个矩形面积加起来减去重叠的即可。
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int sq = (C-A)*(D-B)+(G-E)*(H-F);
long len = (long)Math.min(C,G)-(long)Math.max(A,E);
long we = (long)Math.min(D,H)-(long)Math.max(B,F);
if(len<=0||we<=0){
return sq;
}
return (int)(sq-len*we);
}
}
Rectangle Area——LeetCode的更多相关文章
- Rectangle Area || LeetCode
把交叉点的坐标求出来即可. #define max(a,b) ( (a)>(b)?(a):(b) ) #define min(a,b) ( (a)<(b)?(a):(b) ) int co ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- [LeetCode] 850. Rectangle Area II 矩形面积之二
We are given a list of (axis-aligned) rectangles. Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...
- [LeetCode] 223. Rectangle Area 矩形面积
Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...
- LeetCode 223. 矩形面积(Rectangle Area)
223. 矩形面积 223. Rectangle Area 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. LeetCode2 ...
- [leetcode] Rectangle Area
Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectang ...
- 【leetcode】Contains Duplicate & Rectangle Area(easy)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- 【刷题-LeetCode】223. Rectangle Area
Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectang ...
- leetcode之Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
随机推荐
- JS根据key值获取URL中的参数值,以及把URL的参数转换成json对象
//把url的参数部分转化成json对象 parseQueryString: function (url) { var reg_url = /^[^\?]+\?([\w\W]+)$/, reg_par ...
- JQ 日期格式化
将字符转换为日期格式: function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$ ...
- UITextView/UITextField检测并过滤Emoji表情符号
UITextView/UITextField检测并过滤Emoji表情符号 本人在开发过程中遇到过这种情况,服务器端不支持Emoji表情,因此要求客户端在上传用户输入时,不能包含Emoji表情.在客户端 ...
- I/O多路转接之select
系统提供select函数来实现多路复⽤用输入/输出模型.select系统调用是用来让我们的程序监视 多个文件句柄的状态变化的.程序会停在select这里等待,直到被监视的文件句柄有一个或 多个发生了状 ...
- 找出整数中第k大的数
一 问题描述: 找出 m 个整数中第 k(0<k<m+1)大的整数. 二 举例: 假设有 12 个整数:data[1, 4, -1, -4, 9, 8, 0, 3, -8, 11, 2 ...
- 【POJ3237】【树链剖分】Tree
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- tbody 滚动条
ARRIVALS TO BRISTOL - ENGLAND FLIGHT CODE FROM STA ETA Notes T3 4264 ISLE OF MAN 11:40 11:42 LANDED ...
- c++ undefined reference to mysqlinit
Solved g++ $(mysql_config --cflags) file.cpp -o filename $(mysql_config --libs)
- [转]NodeJS、NPM安装配置步骤(windows版本)
1.windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://nodejs.org/),便可以看到首页的“INSTALL” ...
- apache-php安装mysql简单方法
1.启用mysql功能,在php.ini中 extension=php_mysql.dll extension=php_mysqli.dll 2. 修改extension_dir = "ex ...