223. Rectangle Area (求两矩形重合部分的面积)
2016-10-28 09:41
330 查看
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.
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int sub = 0;
if(A>=G||E>=C||B>=H||F>=D)
sub = 0;
else{
int length1 = 0,length2=0;
if(H<D){
if(B>F)
length1=H-B;
else
length1=H-F;
}
else{
if(F>B)
length1=D-F;
else
length1=D-B;
}
if(A<E){
if(C<G)
length2=C-E;
else
length2=G-E;
}
else{
if(C>G)
length2=G-A;
else
length2=C-A;
}
sub = length1*length2;
}
return (D-B)*(C-A)+(G-E)*(H-F)-sub;
}
}
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.
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int sub = 0;
if(A>=G||E>=C||B>=H||F>=D)
sub = 0;
else{
int length1 = 0,length2=0;
if(H<D){
if(B>F)
length1=H-B;
else
length1=H-F;
}
else{
if(F>B)
length1=D-F;
else
length1=D-B;
}
if(A<E){
if(C<G)
length2=C-E;
else
length2=G-E;
}
else{
if(C>G)
length2=G-A;
else
length2=C-A;
}
sub = length1*length2;
}
return (D-B)*(C-A)+(G-E)*(H-F)-sub;
}
}
相关文章推荐
- LeetCode-223. Rectangle Area (JAVA)求矩形覆盖面积
- leetcode 223. Rectangle Area-矩形面积
- LeetCode 223. Rectangle Area(两个矩形的复合面积计算)
- leetcode 223. Rectangle Area 矩形面积计算
- leetcode_223. Rectangle Area 求两个矩形覆盖的面积之和
- [LeetCode]60. Rectangle Area矩形面积
- 223. Rectangle Area扩展--判断两矩形是否交叉(包括包含的情况)
- Rectangle Area 两个矩形的面积
- 2016华为机试之矩形重合面积
- 已知一个抽象类Shapge,该类中有一个方法GetArea。 要求定义一个Rectangle类,继承Shape类,实现GetArea方法计算矩形面积。
- javascript中矩形的碰撞检测---- 计算碰撞部分的面积
- POJ 1389 Area of Simple Polygons(线段树+扫描线求矩形面积并)
- 求两个矩形重叠部分的面积
- 两个矩形重叠部分面积
- Rectangle Area 两个矩形的面积
- LeetCode----223. Rectangle Area 2个矩形和
- POJ 1389 Area of Simple Polygons(多矩形重叠面积==离散化)
- 【POJ 1389】Area of Simple Polygons(线段树+扫描线,矩形并面积)
- 4.根据下面的主类,编写一个立方体类Cube,该类中有求矩形面积area()和体积volume()的方法,而且每种方法都有一个无参的和有参的方法构成过载。
- 用Rectangle输出一个矩形的周长和面积