zoukankan      html  css  js  c++  java
  • number-of-boomerangs

    https://leetcode.com/problems/number-of-boomerangs/

    package com.company;
    
    import java.util.*;
    
    class Solution {
        public int numberOfBoomerangs(int[][] points) {
            int ret = 0;
    
            for (int i=0; i<points.length; i++) {
                Map<Long, Integer> dMap = new HashMap<>();
                long dist;
                int count;
    
                for (int j=0; j<points.length; j++) {
                    if (i == j) {
                        continue;
                    }
                    dist = (points[i][0]-points[j][0])*(points[i][0]-points[j][0]) +
                            (points[i][1]-points[j][1])*(points[i][1]-points[j][1]);
    
                    count = 0;
                    if (dMap.containsKey(dist)) {
                        count = dMap.get(dist);
                    }
                    count++;
                    dMap.put(dist, count);
                }
                Iterator<Map.Entry<Long, Integer>> iter = dMap.entrySet().iterator();
                while (iter.hasNext()) {
                    int val = iter.next().getValue();
                    ret += val * (val-1);
                }
            }
            return ret;
        }
    }
    
    public class Main {
    
        public static void main(String[] args) throws InterruptedException {
    
            System.out.println("Hello!");
            Solution solution = new Solution();
    
            // Your Codec object will be instantiated and called as such:
            int[][] points = {{0,0},{1,0},{2,0}};
            int ret = solution.numberOfBoomerangs(points);
            System.out.printf("ret:%d
    ", ret);
    
            System.out.println();
    
        }
    
    }
  • 相关阅读:
    C# 设计模式-状态模式
    C# 设计模式-备忘录模式
    C# 设计模式-命令模式
    本地易优安装总结
    视频自动添加字幕
    百度商桥安装
    百度统计
    模板
    百度地图API
    栅格布局的理解
  • 原文地址:https://www.cnblogs.com/charlesblc/p/6039693.html
Copyright © 2011-2022 走看看