223. Rectangle Area
来源:互联网 发布:雾林寒战知乎 编辑:程序博客网 时间:2024/06/08 01:59
一直在寻找一个合适的解决方法,可以各种问题,一个bug接着另一个bug,总是可以找出反例,估计还要再自习斟酌一番。
错误的代码总是能举出反例。
错误代码:
class Solution {public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { pair<int,int> temp1(A,B); pair<int,int> temp2(A,D); pair<int,int> temp3(C,B); pair<int,int> temp4(C,D); pair<int,int> temp5(E,F); pair<int,int> temp6(E,H); pair<int,int> temp7(G,F); pair<int,int> temp8(G,H); vector<pair<int,int>> first; vector<pair<int,int>> second; //first if(A>=E&&A<=G) { if(B>=F&&B<=H) first.push_back(temp1); if(D>=F&&D<=H) first.push_back(temp2); } if(C>=E&&C<=G) { if(B>=F&&B<=H) first.push_back(temp3); if(D>=F&&D<=H) first.push_back(temp4); } //second if(E>=A&&E<=C) { if(F>=B&&F<=D) second.push_back(temp5); if(H>=B&&H<=D) second.push_back(temp6); } if(G>=A&&G<=C) { if(F>=B&&F<=D) second.push_back(temp7); if(H>=B&&H<=D) second.push_back(temp8); } //cal int area=(C-A)*(D-B)+(G-E)*(H-F); //cout<<first.size()<<" "<<second.size()<<" "<<area; if(first.size()==4) return (G-E)*(H-F); else if(second.size()==4) return (C-A)*(D-B); else if(first.size()==0&&second.size()==0) return area; else if(first.size()==1&&second.size()==1) return area-abs(first[0].first-second[0].first)*abs(first[0].second-second[0].second); else if(first.size()==2) { int duplicate=0; if(first[0].first==first[1].first) { if(E>A&&E<C) duplicate=abs(first[0].second-first[1].second)*abs(E-first[0].first); else if(G>A&&G<C) duplicate=abs(first[0].second-first[1].second)*abs(G-first[0].first); else{} } else { if(F>B&&F<D) duplicate=abs(first[0].first-first[1].first)*abs(F-first[0].second); else if(H>B&&H<D) duplicate=abs(first[0].first-first[1].first)*abs(H-first[0].second); else{} } return area-duplicate; } else if(second.size()==2) { int duplicate=0; if(second[0].first==second[1].first) { if(A>E&&A<G) duplicate=abs(second[0].second-second[1].second)*abs(A-second[0].first); else if(C>E&&C<G) duplicate=abs(second[0].second-second[1].second)*abs(C-second[0].first); else{} } else { if(B>F&&B<H) duplicate=abs(second[0].first-second[1].first)*abs(B-second[0].second); else if(D>F&&D<H) duplicate=abs(second[0].first-second[1].first)*abs(D-second[0].second); else{} } return area-duplicate; } else return 0; }};
参考discuss。写出自己的代码,还是数学功底重要啊。。。
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); int right=min(C,G); int up=min(D,H); int down=max(B,F); if(left<right&&up>down) return (C-A)*(D-B)+(G-E)*(H-F)-(right-left)*(up-down); else return (C-A)*(D-B)+(G-E)*(H-F); }};
0 0
- 223. Rectangle Area
- [leetcode] 223.Rectangle Area
- [leetcode] 223. Rectangle Area
- 223. Rectangle Area LeetCode
- #leetcode# 223. Rectangle Area
- 223. Rectangle Area
- leetcode 223. Rectangle Area
- 223. Rectangle Area
- LeetCode--223. Rectangle Area
- 223. Rectangle Area
- LeetCode *** 223. Rectangle Area
- LeetCode 223. Rectangle Area
- leetcode-223. Rectangle Area
- 223. Rectangle Area
- 223. Rectangle Area
- LeetCode-223.Rectangle Area
- 223.[Leetcode]Rectangle Area
- 223. Rectangle Area
- 二维码的生成与解析
- Spring+Mybatis多数据源配置(四)——AbstractRoutingDataSource实现数据源动态切换
- 华为机试-名字的漂亮度
- Android Bluetooth Low Energy(Android低功耗蓝牙)
- Redis 缓存 + Spring 的集成示例
- 223. Rectangle Area
- Windows 剪切板API详解
- 数组和指针相关
- 【修图】
- 廖雪峰python教程阅读之使用disc和set
- Hibernate一级缓存(基于查询分析)
- AE基础汇总
- FZU2204-7
- 程序员面试 刷题推荐