zoukankan      html  css  js  c++  java
  • 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;
        }
    };
    

      

  • 相关阅读:
    CLR Via
    HTML&XML
    SNS研究
    DotNet&C#
    电子商务
    WCF学习
    SQL
    构架设计
    JS&Ajax
    NHibernate之旅系列文章导航
  • 原文地址:https://www.cnblogs.com/xqn2017/p/8569660.html
Copyright © 2011-2022 走看看