Rectangle Area

原创 2015年07月07日 21:01:01

1 题目描述

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.

题目出处:https://leetcode.com/problems/rectangle-area/

2 解题思路

1.总面积 = 矩形A  + 矩形B - A与B相交的部分。

3 源代码

package com.larry.easy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RectangleArea {
	public boolean isIntersected(int A, int B, int C, int D, int E, int F, int G, int H){
		if(A >= G) return false;
		if(F >= D) return false;
		if(E >= C) return false;
		if(B >= H) return false;
		return true;
	}
	
	public int getMidTwo(int a, int b, int c, int d){
		int retn = 0;
		List<Integer> ls = new ArrayList<Integer>();
		ls.add(a);
		ls.add(b);
		ls.add(c);
		ls.add(d);
		Collections.sort(ls);
		retn = ls.get(2) - ls.get(1);
		return retn;
	}
	
	public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
		int totalBef = (C-A)*(D-B) + (G-E)*(H-F);//分别计算两个矩形
		
		boolean isInted = isIntersected(A, B, C, D, E, F, G, H);
		if(isInted) {//如果相交,则减去相交的部分
			int length = getMidTwo(A, C, E, G);
			int width = getMidTwo(B, D, F, H);
			totalBef = totalBef - length*width;
		}
		
        return totalBef;
    }
	public static void main(String[] args) {
		/*int a = -3;
		int b = 0;
		int c = 3;
		int d = 4;
		int e = 0;
		int f = -1;
		int g = 9;
		int h = 2;*/
		/*int a = 0;
		int b = 0;
		int c = 0;
		int d = 0;
		int e = -1;
		int f = -1;
		int g = 1;
		int h = 1;*/
		int a = -2;
		int b = -2;
		int c = 2;
		int d = 2;
		int e = 3;
		int f = 3;
		int g = 4;
		int h = 4;
		
		int A = a;
		int B = b;
		int C = c;
		int D = d;
		int E = e;
		int F = f;
		int G = g;
		int H = h;
		RectangleArea ra = new RectangleArea();
		System.out.println(ra.computeArea(A, B, C, D, E, F, G, H));
	}
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode 223 Rectangle Area 矩形区域面积

计算两个矩形所在2D空间内覆盖的区域面积。

[Leetcode]-Rectangle Area

//题目:Find the total area covered by two rectilinear rectangles in a 2D plane. //Each rectangle is d...

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

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

LeetCode 223 Rectangle Area(矩形面积)

翻译找到在二维平面中两个相交矩形的总面积。每个矩形都定义了其左下角和右上角的坐标。(矩形如下图)假设,总占地面积永远不会超过int的最大值。原文分析这题前天试过,写了一堆判断,终究还是无果……贴几个别...
  • NoMasp
  • NoMasp
  • 2016-01-27 12:17
  • 2230

204. Count Primes \ 223. Rectangle Area

Rectangle Area Count Primes223. Rectangle Areaclass Solution { public: int computeArea(int A, in...

LeetCode Rectangle Area

LeetCode Rectangle Area题目思路刚开始自己写别提WA多少遍了; 后来看到标达真的被惊讶到了; 代码可以这么美;代码自己的#define min(A, B) (A > B ? ...

Leetcode 223 - Rectangle Area(矩形面积交)

题意两个和坐标轴平行的矩形,可能相交,求这两个矩形的总面积。思路关键是确定相交矩形的面积,我们可以通过确定四个顶点,也可以通过长和宽。算法1通过确定四个顶点(实际上是4条边的位置,最上面的边,最下面的...
  • Lzedo
  • Lzedo
  • 2017-03-06 11:00
  • 107

【leetcode】223. Rectangle Area

一、题目解读 Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is...

Rectangle Area - LeetCode 223

题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is d...

Rectangle Area

Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)