当前位置:网站首页>LeetCode_容斥原理_中等_223.矩形面积
LeetCode_容斥原理_中等_223.矩形面积
2022-07-29 11:33:00 【小城老街】
1.题目
给你二维平面上两个由直线构成且边与坐标轴平行/垂直的矩形,请你计算并返回两个矩形覆盖的总面积。
每个矩形由其左下顶点和右上顶点坐标表示:
第一个矩形由其左下顶点 (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
提示:
-104 <= ax1, ay1, ax2, ay2, bx1, by1, bx2, by2 <= 104
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/rectangle-area
2.思路
(1)容斥原理
通过 2 个集合的容斥原理:A ∪ B = A + B - A ∩ B 可知,两个矩形覆盖的总面积 = 两个矩形的面积之和 - 重合部分的面积,所以我们只需要分别两个矩形的面积以及它们重合部分的面积即可,需要注意的是两个矩形可能不重合,即重合部分的面积为 0。
3.代码实现(Java)
//思路1————容斥原理
class Solution {
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
// 矩形 A 的面积
int areaA = (ax2 - ax1) * (ay2 - ay1);
// 矩形 B 的面积
int areaB = (bx2 - bx1) * (by2 - by1);
int x = Math.max(0, Math.min(ax2, bx2) - Math.max(ax1, bx1));
int y = Math.max(0, Math.min(ay2, by2) - Math.max(ay1, by1));
// 矩形 A 和矩形 B 重合部分的面积(可能为 0)
int coincide = x * y;
/* 2 个集合的容斥原理:A ∪ B = A + B - A ∩ B 两个矩形覆盖的总面积 = 两个矩形的面积之和 - 重合部分的面积 */
return areaA + areaB - coincide;
}
}
边栏推荐
- Self collection online computer wallpaper PHP source code v2.0 adaptive end
- 如何开始为您的 Kubernetes 应用程序编写 Helm 图表
- How to start writing helm charts for your kubernetes application
- From scratch Blazor Server (3) - add cookie authorization
- DOD and Dor, two artifacts to reduce "cognitive bias"
- Package Delivery(贪心)
- TCP and UDP
- AMH6.X升级到AMH7.0后,登录后台提示MySQL连接出错怎么解决?
- 【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
- 幸运抽奖系统带后台源码
猜你喜欢
暑假集训week1
Exclusive interview | Cheng Li, chief technology officer of Alibaba: cloud + open source together form a credible foundation for the digital world
[image processing] image skeleton extraction based on central axis transformation with matlab code
面试官培训课件(非常实用的企业内训课件)
2022 latest WiFi master applet independent version 3.0.8
Alluxio为Presto赋能跨云的自助服务能力
基于flask写的一个小商城mall项目
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
Based on the flask to write a small shopping mall project
共建共享数字世界的根:阿里云打造全面的云原生开源生态
随机推荐
1.MySQL数据库的介绍
企业微信客户朋友圈一天可以发多少条?都有哪些限制?如何突破朋友圈可展示人数限制?
mysql single-line, multi-line subquery
Matplotlib Chinese question
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
Function comparison between report control FastReport and stimulus soft
解决idea在debug模式下变得非常慢的问题
『面试知识集锦100篇』1.面试技巧篇丨HR的小心思,你真的懂吗?
Paddlelite compilation and code running through the disk
puzzle(017.5)联动归位
自采集在线电脑壁纸php源码v2.0自适应端
Golang realizes file upload and download
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
Deep understanding of c # delegate into the fast lanes
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
宝塔快速搭建自适应咖啡网站模板与管理系统源码实测
After connect and SQL join in on conditions and where
一次node文件操作过多排查过程总结
JS two array objects for merging and de duplication
Spark efficient data analysis 01. Establishment of idea development environment