447:回旋镖的数量

This commit is contained in:
huangge1199 2021-09-13 14:11:21 +08:00
parent 9f32678584
commit 14836e32f9
3 changed files with 135 additions and 1 deletions

View File

@ -0,0 +1,95 @@
//给定平面上 n 互不相同 的点 points 其中 points[i] = [xi, yi] 回旋镖 是由点 (i, j, k) 表示的元组 其中
// i j 之间的距离和 i k 之间的距离相等需要考虑元组的顺序
//
// 返回平面上所有回旋镖的数量
//
//
// 示例 1
//
//
//输入points = [[0,0],[1,0],[2,0]]
//输出2
//解释两个回旋镖为 [[1,0],[0,0],[2,0]] [[1,0],[2,0],[0,0]]
//
//
// 示例 2
//
//
//输入points = [[1,1],[2,2],[3,3]]
//输出2
//
//
// 示例 3
//
//
//输入points = [[1,1]]
//输出0
//
//
//
//
// 提示
//
//
// n == points.length
// 1 <= n <= 500
// points[i].length == 2
// -10 <= xi, yi <= 10
// 所有点都 互不相同
//
// Related Topics 数组 哈希表 数学 👍 176 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.TwoArray;
import java.util.Arrays;
//447:回旋镖的数量
class NumberOfBoomerangs {
public static void main(String[] args) {
//测试代码
Solution solution = new NumberOfBoomerangs().new Solution();
TwoArray twoArray = new TwoArray("[[0,0],[1,0],[2,0]]", true);
System.out.println(solution.numberOfBoomerangs(twoArray.getArr()));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int numberOfBoomerangs(int[][] points) {
int size = points.length;
int[][] lengths = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
int x = points[j][0] - points[i][0];
int y = points[j][1] - points[i][1];
int sum = x * x + y * y;
lengths[i][j] = sum;
lengths[j][i] = sum;
}
}
int counts = 0;
for (int[] length : lengths) {
Arrays.sort(length);
int count = 0;
int bef = length[0];
for (int i = 1; i < size; i++) {
if (length[i] == bef) {
count++;
} else {
counts += (count + 1) * count;
count = 0;
bef = length[i];
}
}
if (count > 0) {
counts += (count + 1) * count;
}
}
return counts;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,39 @@
<p>给定平面上<em> </em><code>n</code><em> </em><strong>互不相同</strong> 的点 <code>points</code> ,其中 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code><strong>回旋镖</strong> 是由点 <code>(i, j, k)</code> 表示的元组 ,其中 <code>i</code> 和 <code>j</code> 之间的距离和 <code>i</code> 和 <code>k</code> 之间的距离相等(<strong>需要考虑元组的顺序</strong>)。</p>
<p>返回平面上所有回旋镖的数量。</p>
 
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>points = [[0,0],[1,0],[2,0]]
<strong>输出:</strong>2
<strong>解释:</strong>两个回旋镖为 <strong>[[1,0],[0,0],[2,0]]</strong><strong>[[1,0],[2,0],[0,0]]</strong>
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>points = [[1,1],[2,2],[3,3]]
<strong>输出:</strong>2
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>points = [[1,1]]
<strong>输出:</strong>0
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == points.length</code></li>
<li><code>1 <= n <= 500</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>所有点都 <strong>互不相同</strong></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li><li>数学</li></div></div><br><div><li>👍 176</li><li>👎 0</li></div>