关闭

[Leetcode] 223. Rectangle Area 解题报告

标签: Leetcode解题报告Math
174人阅读 评论(0) 收藏 举报
分类:

题目

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.

Rectangle Area

Assume that the total area is never beyond the maximum possible value of int.

思路

一道简单的数学题,关键在于计算出来重合部分的面积,这里重点在于区分各种重合情况。我自己写的代码比较冗长,这里同时贴几个写的很精炼的代码。

代码

1、自己的代码:

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int area1 = (C - A) * (D - B);
    	int area2 = (G - E) * (H - F);
    	if (E >= C || G <= A || H <= B || F >= D) {     // no overlap
    		return area1 + area2;
    	}
    	int width = 0, height = 0;
    	if (E < A) {                                    // update width
    		width = G < C ? (G - A) : (C - A);
    	}
    	else {
    		width = G < C ? (G - E) : (C - E);
    	}
    	if (F < B) {                                    // update height
    		height = H < D ? (H - B) : (D - B);
    	}
    	else {
    		height = H < D ? (H - F) : (D - F);
    	}
    	return area1 + area2 - width * height;
    }
};

2、别人的代码:

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int area = (C - A) * (D - B) + (G - E) * (H - F);  
        if(!(B >= H || F >= D || A >= G ||E >= C)) {  
            int tem = (min(C, G) - max(A, E)) * (min(D, H) -max(B, F));  
            area -= tem;  
        }  
        return area;  
    }
};
class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int left = max(A, E), right = max(min(C, G), left);  
        int bot = max(B, F), top = max(min(D, H), bot);  
        return (C - A) * (D - B) + (G - E) * (H - F) - (right - left) * (top - bot);
    }
};

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

【LeetCode-面试算法经典-Java实现】【223-Rectangle Area(矩形区域)】

【223-Rectangle Area(矩形区域)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-Jun-Chao】原题...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-29 06:39
  • 2619

LeetCode 223. Rectangle Area(两个矩形的复合面积计算)

原题网址:https://leetcode.com/problems/rectangle-area/ Find the total area covered by two rectilinear...
  • jmspan
  • jmspan
  • 2016-04-05 00:48
  • 1072

Leetcode 解题报告

据说刷完leetcode是准备编程面试的第一步。想到明年可能就要开始找工作了,先准备着吧。 计划今年11月份前做完,大概平均每天2-3题,难度应该不大,贵在坚持! 代码放在github上,地址:htt...
  • Wiking__acm
  • Wiking__acm
  • 2016-06-20 15:58
  • 1083

LeetCode-Rectangle Area 解题报告

LeetCode-Rectangle Area 解题报告
  • dd523762588
  • dd523762588
  • 2015-06-29 22:09
  • 244

LeetCode #223 - Rectangle Area - Easy

ProblemFind the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is def...
  • Arcome
  • Arcome
  • 2016-12-14 17:41
  • 167

LeetCode-223. Rectangle Area (JAVA)求矩形覆盖面积

LeetCode-223. Rectangle Area (JAVA)求矩形覆盖面积
  • mine_song
  • mine_song
  • 2017-04-29 22:31
  • 484

[leetcode] 223.Rectangle Area

题目: Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defin...
  • u014673347
  • u014673347
  • 2015-07-24 10:29
  • 201

Leetcode_223_Rectangle Area

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46868363 Find the total area...
  • pistolove
  • pistolove
  • 2015-07-13 22:02
  • 949

Rectangle Area - LeetCode 223

题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is d...
  • bu_min
  • bu_min
  • 2015-06-08 22:04
  • 247

Leetcode 223. Rectangle Area

223. Rectangle Area Total Accepted: 46967 Total Submissions: 150715 Difficulty: Easy Find the to...
  • fantasiasango
  • fantasiasango
  • 2016-09-13 03:26
  • 149
    个人资料
    • 访问:88363次
    • 积分:6321
    • 等级:
    • 排名:第4462名
    • 原创:558篇
    • 转载:10篇
    • 译文:2篇
    • 评论:17条
    最新评论