tmp_code
Posted youdias
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tmp_code相关的知识,希望对你有一定的参考价值。
bool cmpX(vector<int> a,vector<int> b) { return a[0]<b[0]; } bool cmpY(vector<int> a, vector<int> b) { return a[1]<b[1]; } class Solution939 { public: int minAreaRect(vector<vector<int>>& points) { int ret = INT_MAX; for (int i = 0; i < points.size(); i++) { vector<vector<int> > getxs; vector<vector<int> > getys; //得到X值相同的点 getxs = GetXs(points, 0, points[i]); //得到Y值相同的点 getys = GetYs(points, 0, points[i]); int tmparea = GetMiniRect(points, getxs, getys, points[i]); if (ret > tmparea) { ret = tmparea; } } if (ret == INT_MAX) { ret = 0; } return ret; } int GetMiniRect(vector<vector<int> > points, vector<vector<int> > Xs,vector<vector<int> > Ys,vector<int> startpoint) { int ret=INT_MAX; vector<int> tmpx; vector<int> tmpy; //一条水平线上的,按照y排序 sort(Xs.begin(), Xs.end(),cmpY); //一条竖直线上的,按照x排序 sort(Ys.begin(), Ys.end(),cmpX); int x1 = 0; int x2 = 0; int y1 = 0; int y2 = 0; x1 = startpoint[0]; y1 = startpoint[1]; for (int i = 0; i < Xs.size(); i++) { y2 = Xs[i][1]; for (int j = 0; j < Ys.size(); j++) { x2 = Ys[i][0]; vector<int> tmp = vector < int > {x2, y2}; if (std::find(points.begin(), points.end(), tmp) != points.end()) { ret = (x2 - x1)*(y2 - y1); return ret; } else { continue; } } } return ret; } vector<vector<int> > GetXs(vector<vector<int> > points, int begin, vector<int> target) { vector<vector<int> > ret; for (int i = begin; i < points.size(); i++) { if (points[i] == target) { continue; } if (points[i][0] == target[0] && points[i][1] > target[1] ) { ret.emplace_back(points[i]); } } return ret; } vector<vector<int> > GetYs(vector<vector<int> > points, int begin, vector<int> target) { vector<vector<int> > ret; for (int i = begin; i < points.size(); i++) { if (points[i] == target) { continue; } if (points[i][1] == target[1] && points[i][0] > target[0]) { ret.emplace_back(points[i]); } } return ret; } };
以上是关于tmp_code的主要内容,如果未能解决你的问题,请参考以下文章