zoukankan      html  css  js  c++  java
  • 447. Number of Boomerangs

    Problem:

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).

    Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).

    Example:

    Input:

    [[0,0],[1,0],[2,0]]
    
    Output:
    2
    
    Explanation:
    The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]
    

    思路

    Solution (C++):

    int numberOfBoomerangs(vector<vector<int>>& points) {
        if (points.empty() || points[0].empty()) return 0;
        int m = points.size(), count = 0, res = 0;
        vector<int> dis(m, 0);
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < m; ++j) {
                if (i == j) dis[j] = 0;
                dis[j] = distance(points[i], points[j]);                
            }
            sort(dis.begin(), dis.end());
            for (int k = 0; k < m-1; ++k) {
                while (k < m-1 && dis[k] == dis[k+1]) { count++; k++; }
                if (count >= 2)  res += An2(count);
                count = 1;
            }
        }
        return res;
    }
    int distance(vector<int> v1, vector<int> v2) {
        return pow(v2[0]-v1[0], 2) + pow(v2[1]-v1[1], 2);
    }
    int An2(int n) {
        return n * (n-1);
    }
    

    性能

    Runtime: 1416 ms  Memory Usage: 161.7 MB

    思路

    Solution (C++):

    
    

    性能

    Runtime: ms  Memory Usage: MB

    相关链接如下:

    知乎:littledy

    欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可

    作者:littledy
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    WCF三种通信方式
    Linux发布WebApi
    Supervisor Linux程序进程管理
    Centos安装Mongodb
    本地网址连不上远程mysql问题
    .Net之垃圾回收算法
    .Net之托管堆资源分配
    Centos7+ASP.Net Core 运行
    ASP .Net Core 使用 Dapper 轻型ORM框架
    转载 Jquery中AJAX参数详细介绍
  • 原文地址:https://www.cnblogs.com/dysjtu1995/p/12613862.html
Copyright © 2011-2022 走看看