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 ...
随机推荐
- (转)兼容主流浏览器的CSS透明代码
透明往往能产生不错的网页视觉效果下面是兼容主流浏览器的CSS透明代码.transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; - ...
- HTML5 文件域+FileReader 分段读取文件并上传(八)-WebSocket
一.同时上传多个文件处理 HTML: <div class="container"> <div class="panel panel-default&q ...
- vs2010 web 发布
1.在服务器上安装web deploy,这时iis中右侧功能中就多了“导入应用程序” 2.在代码的项目中,点击项目属性,将debug改为release,选择对应的平台.目标平台,主要用来区分32位还是 ...
- Visual C#实现Windows信使服务
现在有很多网络管理软件都具备网络上信息实时传送的功能,虽然有些网络通讯软件功能比较强大,有的软件不仅可以传送文本信息,还可以传送二进制文件等.但 它们都有一个无法克服的缺点,那就是分发比较困难,信息传 ...
- asp.net mvc生命周期学习
ASP.NET MVC是一个扩展性非常强的框架,探究其生命周期对用Mock框架来模拟某些东西,达到单元测试效果,和开发扩展我们的程序是很好的. 生命周期1:创建routetable.把URL映射到ha ...
- 【转】 ios开发之倒计时实现的两种方法
原文:http://blog.csdn.net/kylinbl/article/details/8972261 方法1:使用NSTimer来实现 主要使用的是NSTimer的scheduledTime ...
- $.cookie('name', null) 删除cookie 失效问题
这几天在做网站的用户登录和退出功能,我使用jquery.cookie.js来操作cookie,但是用$.cookie('name', null) 总是失效, 后来使用$.cookie("na ...
- 打开自定义链接新窗口(safari JS prompt的坑!)2016.03.08
很简单的一个小练习,但做的过程中发现safari的一个坑,使用prompt()方法的时候,点击取消和不输入一样,会返回空字符' ',而不是null! 要求: 制作新按钮,"新窗口打开网站&q ...
- hdoj 4310 贪心
不知为毛,过不了 我的代码: #include<stdio.h> int main(){ int n,a[30],b[30],temp,i,j,s1,s2; double c[30]; w ...
- 【转】 C语言自增自减运算符深入剖析
转自:http://bbs.csdn.net/topics/330189207 C语言的自增++,自减--运算符对于初学者来说一直都是个难题,甚至很多老手也会产生困惑,最近我在网上看到一个问题:#in ...