LeetCode 223 Rectangle Area(矩形面积)
翻译
找到在二维平面中两个相交矩形的总面积。
每一个矩形都定义了其左下角和右上角的坐标。
(矩形例如以下图)
如果,总占地面积永远不会超过int的最大值。
原文
分析
这题前天试过,写了一堆推断。终究还是无果……
贴几个别人的解决方式……
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
{
int64_t xmin1 = min( A, C );
int64_t xmax1 = max( A, C );
int64_t ymin1 = min( B, D );
int64_t ymax1 = max( B, D );
int64_t xmin2 = min( E, G );
int64_t xmax2 = max( E, G );
int64_t ymin2 = min( F, H );
int64_t ymax2 = max( F, H );
int64_t xa = min( xmax1, xmax2 ) - max( xmin1, xmin2 );
int64_t ya = min( ymax1, ymax2 ) - max( ymin1, ymin2 );
int64_t z = 0, ca = max( xa, z ) * max( ya, z );
int64_t a1 = (xmax1 - xmin1) * (ymax1 - ymin1);
int64_t a2 = (xmax2 - xmin2) * (ymax2 - ymin2);
return a1 + a2 - ca;
}
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int overlap = (min(C,G)-max(A,E))*(min(D,H)-max(B,F));
if ( min(C,G)<=max(A,E) || min(D,H)<=max(B,F) )
overlap = 0;
return (C-A)*(D-B)+(G-E)*(H-F)-overlap;
}
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int common = (C <= E || A >= G || B >= H || D <= F) ? 0 : (min(C, G) - max(A, E)) * (min(D, H) - max(B, F));
return (C - A) * (D - B) + (G - E) * (H - F) - common;
}
LeetCode 223 Rectangle Area(矩形面积)的更多相关文章
- [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矩形面积
/* 像是一道数据分析题 思路就是两个矩形面积之和减去叠加面积之和 */ public int computeArea(int A, int B, int C, int D, int E, int F ...
- 223 Rectangle Area 矩形面积
在二维平面上计算出两个由直线构成的矩形叠加覆盖后的面积. 假设面积不会超出int的范围. 详见:https://leetcode.com/problems/rectangle-area/descrip ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- (easy)LeetCode 223.Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- Java for LeetCode 223 Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- Java [Leetcode 223]Rectangle Area
题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...
- LeetCode : 223. Rectangle Area
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw
- [LeetCode] 850. Rectangle Area II 矩形面积之二
We are given a list of (axis-aligned) rectangles. Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...
随机推荐
- 介绍Oracle自带的一些ASM维护工具 (kfod/kfed/amdu)
1.前言 ASM(Automatic Storage Management)是Oracle主推的一种面向Oracle的存储解决方式,它是一个管理卷组或者文件系统的软件.眼下已经被RAC环境广泛使用,可 ...
- 智课雅思短语---四、Exploit to the full one’s favorableconditions and avoid unfavorable ones
智课雅思短语---四.Exploit to the full one’s favorableconditions and avoid unfavorable ones 一.总结 一句话总结:扬长避短 ...
- zzulioj--1776--和尚特烦恼2——第几个素数(技巧模拟)
1776: 和尚特烦恼2--第几个素数 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 267 Solved: 100 SubmitStatusWeb ...
- 6.boostTCP通信
客户端 #include <boost/asio.hpp> #include <iostream> #include <stdlib.h> using namesp ...
- TabLayout中Indicator的样式修改
最近写一个项目的时候用到了TabLayout,其中Indicator只是固定的一条横线,样式只能修改Color和Height,没有办法改变形状和宽度等其他信息. 经过查看TabLayout类的源码,发 ...
- ItemTouchHelper(实现RecyclerView上添加拖动排序与滑动删除的所有事情)
简单介绍: ItemTouchHelper是一个强大的工具,它处理好了关于在RecyclerView上添加拖动排序与滑动删除的所有事情.它是RecyclerView.ItemDecoration的子类 ...
- 洛谷P2197 nim游戏模板
Code: #include<iostream> using namespace std; int main(){ int t; cin>>t; while(t--){ int ...
- Hadoop集群配置搭建
环境:Centos 6.9,Hadoop 2.7.1,JDK 1.8.0_161,Maven 3.3.9 前言: 1.配置一台master服务器,两台或多台slave服务器. 2.master可 ...
- ES6学习5 字符串的扩展
1.ES6 为字符串添加了遍历器接口,使得字符串可以被for...of循环遍历. for (let codePoint of 'foo') { console.log(codePoint) } // ...
- POJ2299 树状数组求逆序对
裸题,不多解释. #include<iostream> #include<cstdio> #include<algorithm> #include<cstri ...