LeetCode Number of Boomerangs

原题链接在这里:https://leetcode.com/problems/number-of-boomerangs/

题目:

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 iand 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]]

题解:

对于每一个点,求其余所有点到这点的距离. 距离相同的都放在一起, 用HashMap<Integer, Integer> hm保存. key是distance, value是相同distance的点的个数.

对于这一点不同的组合方法有value * (value-1)种.

每个点的组合方法都加到res中return.

Time COmplexity: O(n^2), n = points.length. Space: O(n).

AC Java:

 1 public class Solution {
 2     public int numberOfBoomerangs(int[][] points) {
 3         if(points == null || points.length == 0){
 4             return 0;
 5         }
 6
 7         int res = 0;
 8         HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
 9         for(int i = 0; i<points.length; i++){
10             for(int j = 0; j<points.length; j++){
11                 if(i == j){
12                     continue;
13                 }
14
15                 int dist = getDistance(points[i], points[j]);
16                 hm.put(dist, hm.getOrDefault(dist, 0)+1);
17             }
18
19             for(int val : hm.values()){
20                 res += val * (val-1);
21             }
22             hm.clear();
23         }
24
25         return res;
26     }
27
28     private int getDistance(int [] p, int [] q){
29         int dx = p[0] - q[0];
30         int dy = p[1] - q[1];
31         return dx*dx + dy*dy;
32     }
33 }
时间: 2024-08-03 01:52:39

LeetCode Number of Boomerangs的相关文章

Leetcode 447. Number of Boomerangs JAVA语言

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 iand j equals the distance between i and k (the order of the tuple matters). Find the number of boomerangs. Y

[LeetCode]447 Number of Boomerangs

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 iand j equals the distance between i and k (the order of the tuple matters). Find the number of boomerangs. Y

2016.5.16——leetcode:Number of 1 Bits ,

leetcode:Number of 1 Bits 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n-1)[n = n & (n-1)]的用处 题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For exam

Number of Boomerangs

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.

447. Number of Boomerangs 回力镖数组的数量

[抄题]: 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 boomer

447. Number of Boomerangs

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.

[LeetCode] Number Complement 补数

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading ze

[LeetCode] Number of Digit One 数字1的个数

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow.

LeetCode Number of Longest Increasing Subsequence

原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longe