LeetCode Rectangle Area (技巧)
题意:
分别给出两个矩形的左下点的坐标和右上点的坐标,求他们覆盖的矩形面积?
思路:
不需要模拟,直接求重叠的部分的长宽就行了。问题是如果无重叠部分,注意将长/宽给置为0。
class Solution {
public:
int getArea(int A,int B,int C,int D){return (C-A)*(D-B);}
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
{
int x= min(C,G)>max(A,E)? min(C,G)-max(A,E) : ;
int y= min(D,H)>max(B,F)? min(D,H)-max(B,F) : ;
return getArea(A,B,C,D) + getArea(E,F,G,H) - x*y;
}
};
AC代码
LeetCode Rectangle Area (技巧)的更多相关文章
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- [leetcode] Rectangle Area
Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectang ...
- LeetCode——Rectangle Area
Description:https://leetcode.com/problems/rectangle-area/ public class Solution { public int compute ...
- [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 223. 矩形面积(Rectangle Area)
223. 矩形面积 223. Rectangle Area 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. LeetCode2 ...
- 【leetcode】Contains Duplicate & Rectangle Area(easy)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- 【刷题-LeetCode】223. Rectangle Area
Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectang ...
- [LeetCode] Rectangle Overlap 矩形重叠
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...
随机推荐
- 开源软件架构总结之——Bash(readline做输入交互式,词法语法分析,进程交互)
第3章 The Bourne-Again Shell Bash的主要组件:输入处理,解析,单词展开(word expansion)和其他命令处理,管道(pipeline)中的命令执行.这些组件构成一个 ...
- ruby学习网站
Ruby官方中文网(推荐): https://www.ruby-lang.org/zh_cn/ 国内非常不错的Ruby学习教程网站(推荐): http://www.yiibai.com/ruby Ru ...
- 关于Android的onResume的2点体会(程序切换之后恢复状态)
Android有点儿差劲:按home键之后,立即长按home键选择程序切换回来,居然activity就跑回初始状态去了. 我的程序里面有2个webview,2个按钮,我做到把他们都恢复了. 1 web ...
- vsftp 配置
安装和基本配置网上很多文章,但他们的最终效果不是我想要的: 我想要的是,ftp上传的文件用户可以通过apache的http服务访问,也就是ftp上传的文件可以通过浏览器访问,并且可以通过ftp客户端修 ...
- mysql.default_socket 和 mysqli.default_socket
php.ini 里面mysql.default_socket 和 mysqli.default_socket 设置必须要和mysql里面的值一样,否则连接mysql会提示错误
- Spring中的Jdbc事务管理
Spring提供了对事务的声明式事务管理,只需要在配置文件中做一些配置,即可把操作纳入到事务管理当中,解除了和代码的耦合. Spring声明式事务管理,核心实现就是基于Aop. Spring声明式事务 ...
- C语言:文件操作
以附加方式打开文件,输入数据,关闭文件. #include<stdio.h> #include<stdlib.h> int main() { FILE *fp = NULL; ...
- HDU 1693 二进制表示的简单插头dp
题目大意: 找到多条回路覆盖所有非障碍格子,问这样回路的种数 这里的插头与URAL1519 不一样的是 只要管它是否存在即可,只需要1个二进制位表示状态 #include <cstdio> ...
- Failed to instantiate the default view controller for UIMainStoryboardFile 'Main'
给UITableViewController 展示数据时候 删除系统自带viewController 然后拖过来一个UITableViewController 指定class后没有指定main入口 报 ...
- 赋值运算符、拷贝初始化和this指针
一.赋值运算符和拷贝构造函数(重载技术) 赋值运算符和拷贝构造函数有编译器默认提供,但如果想做更复杂的事,需要重载. 1.下面用一个简单的例子先区分一下赋值运算符和拷贝构造函数: #include&l ...