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;
        }
    };
    

      

  • 相关阅读:
    冗余链接-684-并查集
    Chrome浏览器进程
    BFC布局规则
    Front-end 前端优化总结
    Flex弹性布局
    Browse兼容性问题
    组合关系与组合模式
    YUI3组件框架之plugin
    javascript数据类型及转换
    矩阵打印
  • 原文地址:https://www.cnblogs.com/xqn2017/p/8569660.html
Copyright © 2011-2022 走看看