当前位置:网站首页>Leetcode-223: rectangular area
Leetcode-223: rectangular area
2022-06-24 10:24:00 【Ugly and ugly】
Title Description
Here you are The two sides on the surface are composed of straight lines and the edges are parallel to the coordinate axis / Vertical rectangle , Please calculate and return the total area covered by two rectangles .
Each rectangle is represented by its lower left vertex and upper right vertex coordinates :
The first rectangle consists of its lower left vertex (ax1, ay1) And the top right vertex (ax2, ay2) Definition .
The second rectangle consists of its lower left vertex (bx1, by1) And the top right vertex (bx2, by2) Definition .
Example
Example 1:
Input :ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
Output :45
Example 2:
Input :ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
Output :16
The problem solving process
Ideas and steps
(1) General ideas , First calculate the area of two rectangles , Then judge whether it is necessary to subtract the area of the overlapping part according to whether there is overlap ;
(2) If there is overlap , Then you need to calculate the coordinates of the overlapping part .
Code display
public class ComputeArea {
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int area1 = (ax2 - ax1) * (ay2 - ay1);
int area2 = (bx2 - bx1) * (by2 - by1);
if (bx1 >= ax2 || by1 >= ay2 || bx2 <= ax1 || by2 <= ay1) {
// No overlap
return area1 + area2;
}
int ax1_ = Math.max(ax1, bx1);
int ay1_ = Math.max(ay1, by1);
int ax2_ = Math.min(ax2, bx2);
int ay2_ = Math.min(ay2, by2);
int area3 = (ax2_ - ax1_) * (ay2_ - ay1_);
return area1 + area2 - area3;
}
public static void main(String[] args) {
System.out.println(new ComputeArea().computeArea(-3,0,3,4,0,-1,9,2));
}
}
边栏推荐
- uniapp实现点击拨打电话功能
- Go language development environment setup +goland configuration under the latest Windows
- Machine learning perceptron and k-nearest neighbor
- SQL Server AVG function rounding
- 记录一下MySql update会锁定哪些范围的数据
- numpy.logical_and()
- 线程的六种状态
- 为什么 JSX 语法这么香?
- numpy. logical_ and()
- [db2] sql0805n solution and thinking
猜你喜欢

Using pandas to read SQL server data table

SQL Sever中的窗口函数row_number()rank()dense_rank()

YOLOv6:又快又准的目标检测框架开源啦

canvas掉落的小球重力js特效动画

H5网页如何在微信中自定义分享链接

植物生长h5动画js特效

413 binary tree Foundation

机器学习——感知机及K近邻

Uniapp develops wechat official account, and the drop-down box selects the first one in the list by default

【Energy Reports期刊发表】2022年能源与环境工程国际会议(CFEEE 2022)
随机推荐
被困英西中学的师生安全和食物有保障
5. dish management business development
Machine learning perceptron and k-nearest neighbor
uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。
What are the characteristics of EDI local deployment and cloud hosting solutions?
静态链接库和动态链接库的区别
解决Deprecated: Methods with the same name as their class will not be constructors in报错方案
上升的气泡canvas破碎动画js特效
2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
JMeter接口测试工具基础— 使用Badboy录制JMeter脚本
411 stack and queue (20. valid parentheses, 1047. delete all adjacent duplicates in the string, 150. inverse Polish expression evaluation, 239. sliding window maximum, 347. the first k high-frequency
Floating point notation (summarized from cs61c and CMU CSAPP)
SSM整合
Using pandas to read SQL server data table
解决微信小程序rich-text富文本标签内部图片宽高自适应的方法
leetCode-1823: 找出游戏的获胜者
SQL Server AVG function rounding
Uniapp develops wechat official account, and the drop-down box selects the first one in the list by default
【IEEE出版】2022年智能交通与未来出行国际会议(CSTFM 2022)
415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)
Input :ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2