Ai
1 Star 0 Fork 0

徐长贺/Leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
_356.java 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
Fisher Coder 提交于 2019-01-26 00:18 +08:00 . refactor 356
package com.fishercoder.solutions;
import java.util.HashSet;
import java.util.Set;
/**
* 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.
Example 1:
Given points = [[1,1],[-1,1]], return true.
Example 2:
Given points = [[1,1],[-1,-1]], return false.
Follow up:
Could you do better than O(n2)?
Hint:
Find the smallest and largest x-value for all points.
If there is a line then it should be at y = (minX + maxX) / 2.
For each point, make sure that it has a reflected point in the opposite side.
*/
public class _356 {
public static class Solution1 {
/** credit: https://discuss.leetcode.com/topic/48172/simple-java-hashset-solution */
public boolean isReflected(int[][] points) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
Set<String> set = new HashSet<>();
for (int[] point : points) {
max = Math.max(max, point[0]);
min = Math.min(min, point[0]);
String str = point[0] + "a" + point[1];
set.add(str);
}
int sum = max + min;
for (int[] p : points) {
String str = (sum - p[0]) + "a" + p[1];
if (!set.contains(str)) {
return false;
}
}
return true;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/isulong/Leetcode.git
git@gitee.com:isulong/Leetcode.git
isulong
Leetcode
Leetcode
master

搜索帮助