完美矩形问题的求解思路:(要达到完美矩形时,必须满足的条件)
1、所有的矩形的面积之和等于完美矩形的面积
2、完美矩形的边角四个点的个数都必须是一
3、除了边角四个点外,其他点的个数都必须为偶数
class Solution { public boolean isRectangleCover(int[][] rectangles) { int count = rectangles.length; int sum = 0; int max_x = rectangles[0][2]; int max_y = rectangles[0][3]; int min_y = rectangles[0][1]; int min_x = rectangles[0][0]; String str1 = null; String str2 = null; String str3 = null; String str4 = null; Integer t1 = null; Integer t2 = null; Integer t3 = null; Integer t4 = null; Map<String,Integer> map = new HashMap<>(); for(int i=0;i<count;i++){ str1 = rectangles[i][0]+"_"+rectangles[i][1]; str2 = rectangles[i][2]+"_"+rectangles[i][3]; str3 = rectangles[i][0]+"_"+rectangles[i][3]; str4 = rectangles[i][2]+"_"+rectangles[i][1]; t1 = map.get(str1); t2 = map.get(str2); t3 = map.get(str3); t4 = map.get(str4); map.put(str1, t1==null?1:++t1); map.put(str2, t2==null?1:++t2); map.put(str3, t3==null?1:++t3); map.put(str4, t4==null?1:++t4); if(rectangles[i][2]>max_x){ max_x = rectangles[i][2]; } if(rectangles[i][0]<min_x){ min_x = rectangles[i][0]; } if(rectangles[i][3]>max_y){ max_y = rectangles[i][3]; } if(rectangles[i][1]<min_y){ min_y = rectangles[i][1]; } sum += (rectangles[i][3]-rectangles[i][1])*(rectangles[i][2]-rectangles[i][0]); } str1 = max_x+"_"+max_y; str2 = min_x+"_"+min_y; str3 = max_x+"_"+min_y; str4 = min_x+"_"+max_y; t1 = map.get(str1); t2 = map.get(str2); t3 = map.get(str3); t4 = map.get(str4); if(t1!=null&&t2!=null&&t3!=null&&t4!=null&&t1==1&&t2==1&&t3==1&&t4==1&&sum==(max_x-min_x)*(max_y-min_y)) { map.remove(str1); map.remove(str2); map.remove(str3); map.remove(str4); for(Integer value : map.values()) { if((value&1)!=0) { return false; } } }else { return false; } return true; } }