xqn2017

导航

447. Number of Boomerangs

原题:

447. Number of Boomerangs

解题:

先固定一个点A,假设有B,C两点到A的距离一样,那么排列方式是ABC,ACB,共2种,如果有B,C,D三个点到A的距离都相等,那么排列方式是ABCD,ABDC,ACBD,ACDB,ADBC,ADCB共6种,其实就是有N个点到点A的距离相等,那么排列组合就是N(N-1),那么就可以依次把所有的点遍历一遍,将其当做固定的点A,然后每次遍历统计结果,最后相加得到最后的结果

AC代码:

class Solution {
public:
    int numberOfBoomerangs(vector<pair<int, int>>& points) 
	{
        int size = points.size();
		int count = 0;
		int i,j;
		int x,y;	
		for(i = 0;i < size; i++)
		{
			map <int,int> maptmp;
			for(j = 0; j < size; j++)
			{
				x = points[i].first - points[j].first;
				y = points[i].second - points[j].second;
				maptmp[x*x+y*y]++;
			}
			map <int,int>::iterator it;
			for(it = maptmp.begin();it != maptmp.end(); it++)
			{
				count += it->second*(it->second - 1);
			}
		}
	
		return count;
    }
};

  

posted on 2018-03-14 19:42  xqn2017  阅读(117)  评论(0编辑  收藏  举报