日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

447. Number of Boomerangs

發布時間:2024/7/19 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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. 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: 2Explanation: The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]

鏈接:https://leetcode.com/problems/number-of-boomerangs/#/description

3/25/2017

自己的算法超時了,看別人答案

然而,基本上除了計算距離之外,不需要記錄很多,因為本身答案需要的也就是數量而不是index

有時候需要想一想有沒有簡單的方法,想不到再硬來。

https://discuss.leetcode.com/topic/66587/clean-java-solution-o-n-2-166ms/2

回憶著這個答案,還是錯了,真正理解了一下:

注意加count和map.clear()是在每個i里都進行的。為什么?當有從一個頂點開始相同的距離時,這個頂點就是i為index的點。而另外一個點就是相同距離里的其他點,用排列數計算。

1 public int numberOfBoomerangs(int[][] points) { 2 int res = 0; 3 4 Map<Integer, Integer> map = new HashMap<>(); 5 for(int i=0; i<points.length; i++) { 6 for(int j=0; j<points.length; j++) { 7 if(i == j) 8 continue; 9 10 int d = getDistance(points[i], points[j]); 11 map.put(d, map.getOrDefault(d, 0) + 1); 12 } 13 14 for(int val : map.values()) { 15 res += val * (val-1); 16 } 17 map.clear(); 18 } 19 20 return res; 21 } 22 23 private int getDistance(int[] a, int[] b) { 24 int dx = a[0] - b[0]; 25 int dy = a[1] - b[1]; 26 27 return dx*dx + dy*dy; 28 }

更多討論:

https://discuss.leetcode.com/category/574/number-of-boomerangs

轉載于:https://www.cnblogs.com/panini/p/6621957.html

總結

以上是生活随笔為你收集整理的447. Number of Boomerangs的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。