当前位置:网站首页>leetCode-223: 矩形面积
leetCode-223: 矩形面积
2022-06-24 09:43:00 【文丑颜不良啊】
题目描述
给你二维 面上两个由直线构成且边与坐标轴平行/垂直的矩形,请你计算并返回两个矩形覆盖的总面积。
每个矩形由其左下顶点和右上顶点坐标表示:
第一个矩形由其左下顶点 (ax1, ay1) 和右上顶点 (ax2, ay2) 定义。
第二个矩形由其左下顶点 (bx1, by1) 和右上顶点 (bx2, by2) 定义。
示例
示例 1:
输入:ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
输出:45
示例 2:
输入:ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
输出:16
解题过程
思路及步骤
(1)一般思路,先计算两个矩形的面积,再根据是否有重叠去判断是否需要减去重叠部分的面积;
(2)如果有重叠,则需要计算重叠部分的各个坐标。
代码展示
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) {
// 无重叠
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));
}
}
边栏推荐
- Wechat applet learning to achieve list rendering and conditional rendering
- p5.js千纸鹤动画背景js特效
- Cookie encryption 4 RPC method determines cookie encryption
- Recursive traversal of 414 binary tree
- The great charm of cookies
- Machine learning perceptron and k-nearest neighbor
- 小程序 rich-text中图片点击放大与自适应大小问题
- 利用pandas读取SQL Sever数据表
- Wechat cloud hosting launch public beta: in the appointment of the publicity meeting
- Error reading CSV (TSV) file
猜你喜欢

uniapp 开发微信公众号,下拉框默认选中列表第一个

大中型企业如何构建自己的监控体系

美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架

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

SSH Remote Password free login

正规方程、、、

uniapp实现点击拨打电话功能

NVIDIA's CVPR 2022 oral is on fire! 2D images become realistic 3D objects in seconds! Here comes the virtual jazz band!

利用pandas读取SQL Sever数据表

How does home office manage the data center network infrastructure?
随机推荐
学习使用phpstripslashe函数去除反斜杠
数组无缝滚动demo
Error reading CSV (TSV) file
How large and medium-sized enterprises build their own monitoring system
Binary tree part I
415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)
SQL Sever关于like操作符(包括字段数据自动填充空格问题)
Is there a reliable and low commission futures account opening channel in China? Is it safe to open an account online?
涂鸦智能携多款重磅智能照明解决方案,亮相2022美国国际照明展
416 binary tree (first, middle and last order traversal iteration method)
Go language development environment setup +goland configuration under the latest Windows
Floating point notation (summarized from cs61c and CMU CSAPP)
5.菜品管理业务开发
numpy.logical_and()
411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)
Top issue tpami 2022! Behavior recognition based on different data modes: a recent review
简单的价格表样式代码
Network of test and development - Common Service Protocols
卷妹带你学jdbc---2天冲刺Day1
Canvas draw picture
输入:ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2