代码(Python3) classSolution:defcomputeArea(self,ax1:int,ay1:int,ax2:int,ay2:int,bx1:int,by1:int,bx2:int,by2:int)->int:# x 表示矩形 x 方向相交长度。初始化为 0 ,表示不相交,方便后续处理x:int=0# 如果 x 方向相交,则更新相交长度ifax1<=bx2andbx1<=ax2:x=min(ax2,bx2)-max(ax1,...
Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.Assume that the total area is never beyond the maximum possible value of int....
intcomputeArea(intA,intB,intC,intD,intE,intF,intG,intH) { intdownx,downy,upx,upy; intcrossArea; intall; downx=max(A,E); downy=max(B,F); upx =min(C,G); upy =min(D,H); if(downx>upx||downy>upy)crossArea=0; elsecrossArea=(upx-downx)*(upy-downy); all=(C-A)*(D-...
ref:http://bookshadow.com/weblog/2015/06/08/leetcode-rectangle-area/ 用最小的右端点减去最大的左端点. 例如对于[A,C] 与[E,G], 求相交长度公式为max(min(C, G) - max(A, E), 0), 这里用嘴简单的那种AECG这种相交的case来记这个公式。右端点的min减去左端点的max,再与0取max class Solution...
题目地址: https://leetcode.com/problems/rectangle-area/description/ 题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. ...
Leetcode - Rectangle Area My code: publicclassSolution{publicintcomputeArea(intA,intB,intC,intD,intE,intF,intG,intH){intarea=(D-B)*(C-A)+(H-F)*(G-E);if(A>=G||B>=H||C<=E||D<=F){returnarea;}inttop=Math.min(D,H);intright=Math.min(C,G);intbottom=Math.max(B,F);...
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. 思路: 求两个矩形的覆盖面积。如果两个矩形没有交叉,那么覆盖面积就是两个矩形的面积之和;如果有交叉,那么结果等于两个矩形的...
223. 矩形面积 - 给你 二维 平面上两个 由直线构成且边与坐标轴平行/垂直 的矩形,请你计算并返回两个矩形覆盖的总面积。 每个矩形由其 左下 顶点和 右上 顶点坐标表示: * 第一个矩形由其左下顶点 (ax1, ay1) 和右上顶点 (ax2, ay2) 定义。 * 第二个矩形由其左下顶点 (
思路 首先判断是否有重合,然后分两种情况: 如果没有重合,则返回两个面积和 有重合,总面积 - 重合面积 代码 classSolution{public:intcomputeArea(intA,intB,intC,intD,intE,intF,intG,intH){intarea1=(D-B)*(C-A);intarea2=(H-F)*(G-E);intleft=max(A,E);intright=min(C,G);intdown=max(B...
area, leftborder, rightborder, current, stack = 0, 0, 0, 0, [] stack.append(0) for i in range(1, length): # 当栈为空,或者当前元素比栈顶元素大则直接压入栈. # 栈中元素是递增的,则针对栈中的元素,其前一个元素就是其左边界. ...