# [LeetCode] 356. Line Reflection

Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given points symmetrically, in other words, answer whether or not if there exists a line that after reflecting all points over the given line the set of the original points is the same that the reflected ones.

Note that there can be repeated points.

Could you do better than O(n^2)?

Example 1:

Input: points = [[1,1],[-1,1]]
Output: true
Explanation: We can choose the line x = 0.


Example 2:

Input: points = [[1,1],[-1,-1]]
Output: false
Explanation: We can't choose a line.

Constraints:

• n == points.length
• 1 <= n <= 10^4
• -10^8 <= points[i][j] <= 10^8

• 记录一下横坐标的最大值和最小值
• 将每个坐标都变成字符串存入hashmap

Java实现

 1 class Solution {
2     public boolean isReflected(int[][] points) {
3         int max = Integer.MIN_VALUE;
4         int min = Integer.MAX_VALUE;
5         HashSet<String> set = new HashSet<>();
6         for (int[] p : points) {
7             max = Math.max(max, p[0]);
8             min = Math.min(min, p[0]);
9             String str = p[0] + "a" + p[1];
11         }
12
13         int sum = max + min;
14         for (int[] p : points) {
15             String str = (sum - p[0]) + "a" + p[1];
16             if (!set.contains(str)) {
17                 return false;
18             }
19         }
20         return true;
21     }
22 }

356. Line Reflection

447. Number of Boomerangs

LeetCode 题目总结

posted @ 2021-08-04 23:11  CNoodle  阅读(10)  评论(0编辑  收藏  举报