LeetCode(41)-Rectangle Area
题目:
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 area = (D - B) * (C - A) + (H - F) * (G - E);
if (A >= G || B >= H || C <= E || D <= F)
{
return area;
}
int top = Math.min(D, H);
int right = Math.min(C, G);
int bottom = Math.max(B, F);
int left = Math.max(A, E);
return area - (top - bottom) * (right - left);
}
}
LeetCode(41)-Rectangle Area的更多相关文章
- [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之Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- Java for LeetCode 223 Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- (easy)LeetCode 223.Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- leetcode:Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- Java [Leetcode 223]Rectangle Area
题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...
- leetcode 850. Rectangle Area II
给定一些矩形2 求覆盖面积 矩形不超过200个 1 算法1 朴素思想 虽然朴素但是代码却有意思 利用容斥原理 复杂度高达 N*2^N class Solution: def intersect(rec ...
- LeetCode : 223. Rectangle Area
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw
随机推荐
- iOS注册远程推送消息证书后提示此证书签发者无效的解决办法
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们在编写关于远程推送消息的App时需要注册一个相关的证书,我 ...
- [ExtJS5学习笔记]第六节 Extjs的类系统Class System命名规则及定义和调试
本文地址: http://blog.csdn.net/sushengmiyan/article/details/38479079 本文作者:sushengmiyan ----------------- ...
- Android Demo 下拉刷新+加载更多+滑动删除
小伙伴们在逛淘宝或者是各种app上,都可以看到这样的功能,下拉刷新和加载更多以及滑动删除,刷新,指刷洗之后使之变新,比喻突破旧的而创造出新的,比如在手机上浏览新闻的时候,使用下拉刷新的功能,我们可以第 ...
- 4.QT中进程操作,线程操作
QT中的线程操作 T19Process.pro SOURCES += \ main.cpp CONFIG += C++11 main.cpp #include <QCoreApplicat ...
- 【linux学习笔记】Sublime Text3支持GB2312和GBK编码以及中文输入法
几天在ubuntu15.10下使用Sublime Text3发现中文乱码,以及不能使用中文输入法(搜狗输入法linux版)的问题,捣鼓了半天,终于完善了,下面po一下我的解决方案. 一.支持GB231 ...
- (六十六)TableView内容超过一屏时滚动到屏幕底部的方法
假设数据放置在self.chatMessage数组内,只需要让tableView滚动到最后一条数据底部即可,调用scrollToRowAtIndexPath方法: [_tableView reload ...
- Android项目-高考作文项目架构(三)
上一篇我们讲到了, Http Json的功能的抽取. 如果我们请求的是一个列表的数据呢? 我们使用那个功能就不是很好. 因为一个列表, 还有很多其他功能(比如每个listView都需要setAdap ...
- 关于Class文件
什么是Class文件 Java人对class文件肯定很熟悉了,它是Java源码编译后的产物.JVM运行时负责加载class文件,并根据class定义的执行逻辑运行.java为了将硬件底层的差异屏蔽掉, ...
- python字符串与数字类型转化
数字转字符串:str(数字),如str(10) 相反:int(字符串),如int('10') 另外,import string后 用string.atoi('100',base),转换为int,bas ...
- [RDLC]一步一步教你使用RDLC(一)
一:加数据集,并且命名为Quotation,如下图所示: 二: 添加一张报表,命名为Quotation,如下图所示: 向报表中添加"表"这一项,如下图所示: 这时就弹出一个选择数据 ...