356. Line Reflection
首先找到X方向的中点,如果中点是一个点,那么分别从这个点开始往左右找就行;如果是一个区间,比如1 2之间,那么首先总点数得是偶数,然后以1和2往左右两边找就行。。
找的时候,有3种情况:
同时没找到,继续;
一个找到,一个没找到,FALSE;
同时找到,左边找到的每个点,必须对应一个右边找到的每个点,纵坐标相同。
所以构架的时候,我用的
Map<Integer,Set>
Key是X坐标,Value是一个SET,表示横坐标为X的所有点。
public class Solution {
public boolean isReflected(int[][] points)
{
Map<Integer,Set<Integer>> map = new HashMap<Integer,Set<Integer>>();
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for(int i = 0; i < points.length; i++)
{
int x = points[i][0];
max = Math.max(x,max);
min = Math.min(x,min);
int y = points[i][1];
if(map.containsKey(x))
{
map.get(x).add(y);
}
else
{
Set<Integer> tempSet = new HashSet<>();
tempSet.add(y);
map.put(x,tempSet);
}
}
int left;
int right;
// mid line is between 2 points
if((max+min) % 2 != 0)
{
if(points.length%2 != 0) return false;
if(max+min> 0)
{
left = (max+min)/2;
right = (max+min)/2 + 1;
}
else
{
right = (max+min)/2;
left = right -1;
}
}
else
{
left = (max+min)/2;
right = left;
}
while(left >= min && right <= max)
{
if(map.containsKey(left) && map.containsKey(right))
{
Set<Integer> l = map.get(left);
Set<Integer> r = map.get(right);
if(l.size() != r.size()) return false;
for(Integer i: l)
{
if(!r.contains(i)) return false;
}
left--;
right++;
}
else if(map.containsKey(left) || map.containsKey(right))
{
return false;
}
else
{
left--;
right++;
}
}
return left < min && right > max;
}
}
提示好像也是这么个意思。。但是没搞懂提示里说的N²是怎么个做法。。
356. Line Reflection的更多相关文章
- Leetcode: Line Reflection
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given ...
- [LeetCode] Line Reflection 直线对称
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given ...
- [Swift]LeetCode356. 直线对称 $ Line Reflection
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given ...
- Line Reflection -- LeetCode
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- LeetCode分类-前400题
1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...
- 算法与数据结构基础 - 哈希表(Hash Table)
Hash Table基础 哈希表(Hash Table)是常用的数据结构,其运用哈希函数(hash function)实现映射,内部使用开放定址.拉链法等方式解决哈希冲突,使得读写时间复杂度平均为O( ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- 安卓开发之viewpager学习(头条显示)
activity_luancher.xml代码如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res ...
- const详解
详解C++中的const关键字
- grep操作
这个程序的名称来自Unix文本编辑器ed类似操作的命令: g/re/p 这个命令搜索整个文件中匹配给定正则表达式的文本行,并显示出来.有很多不同的命令行用于改变grep的默认行为,包括显示出不匹配的文 ...
- 安装完 MySQL 后必须调整的 10 项配置
原文出处: mysqlperformanceblog 译文出处:开源中国 欢迎分享原创到伯乐头条 当我们被人雇来监测MySQL性能时,人们希望我们能够检视一下MySQL配置然后给出一些提高建议 ...
- chrome调试状态下动态加载的js
在js文件中加入 //@ sourceURL=文件名.js
- 在vs code中使用ftp-sync插件实现客户端与服务器端代码的同步
在vs code中使用ftp-sync插件实现客户端与服务器端代码的同步 下载安装 vscode-ftp-sync 插件. 安装方法1. Ctrl+Shift+P 输入 ext install [插件 ...
- PHP-HTML重要知识点笔记
1.用frameset.frame和iframe还实现多窗口 2.在图片上利用映射距离usemap来实现按钮跳转.------第8尾集 3.表单必须要有name和value,因为抓包的时候,可发现必须 ...
- shell脚本获取mysql插入数据自增长id的值
shell脚本获取mysql插入数据自增长id的值 在shell脚本中我们可以通过last_insert_id()获取id值,但是,需要注意的是,该函数必须在执行插入操作的sql语句之后,立即调用,否 ...
- 3.1决策树理论--python深度机器学习
参考彭亮老师的视频教程:转载请注明出处及彭亮老师原创 视频教程: http://pan.baidu.com/s/1kVNe5EJ 0. 机器学习中分类和预测算法的评估: 准确率 速度 强壮行 ...
- vim代码折叠功能
问题:怎样在vim中实现代码折叠功能? 解决方法:直接使用vim自带的快捷键和命令,便可以实现功能强大的折叠 小试折叠: 1 :set fdm=marker 在vim中执行该命令 2 5G 将 ...