当前位置:网站首页>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));
}
}
边栏推荐
- 形状变化loader加载jsjs特效代码
- Machine learning - principal component analysis (PCA)
- 线程的六种状态
- 解决Deprecated: Methods with the same name as their class will not be constructors in报错方案
- CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance
- Error reading CSV (TSV) file
- 415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)
- What are the characteristics of EDI local deployment and cloud hosting solutions?
- uniapp实现禁止video拖拽快进
- SSM integration
猜你喜欢
随机推荐
leetCode-面试题 16.06: 最小差
涂鸦智能携多款重磅智能照明解决方案,亮相2022美国国际照明展
416 binary tree (first, middle and last order traversal iteration method)
上升的气泡canvas破碎动画js特效
如何在一个页面上使用多个KindEditor编辑器并将值传递到服务器端
[input method] so far, there are so many Chinese character input methods!
2.登陆退出功能开发
Appium自动化测试基础 — 移动端测试环境搭建(一)
leetCode-498: 对角线遍历
Status of the thread pool
numpy.logical_or
Flink checkPoint和SavePoint
Six states of threads
美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架
牛客-TOP101-BM29
使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?
Uniapp implementation forbids video drag fast forward
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
Graffiti smart brings a variety of heavy smart lighting solutions to the 2022 American International Lighting Exhibition
leetCode-1051: 高度检查器
Input :ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2







