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 ...
随机推荐
- 内联式css样式,直接写在现有的HTML标签中
CSS样式可以写在哪些地方呢?从CSS 样式代码插入的形式来看基本可以分为以下3种:内联式.嵌入式和外部式三种.这一小节先来讲解内联式. 内联式css样式表就是把css代码直接写在现有的HTML标签中 ...
- C# 异步操作
在程序中,普通的方法是单线程的.但中途如果有大型的操作,比如读取大文件,大批量操作数据库,网络传输等,都会导致程序阻塞,表现在界面上就是程序卡或者死掉,界面元素不动了,不响应了.C#异步调用很好的解决 ...
- (JavaScript实现)页面无操作倒计时退出
项目前端页面需要实现,页面没人操作进入倒计时,以下为前端代码实现. //设置(倒计时功能)开关 var _mouseActiveListener_flag = true; beforecount:触发 ...
- hdoj 2601(判断N=i*j+i+j)
Problem E Time Limit : 6000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- c++面试(一)
1.在c++中可以通过"::"来直接操作全局变量. 2.i++与++i效率的比较. (1)內建数据类型时,他们的效率差别不大. (2)自定义数据类型(类等)的情况,(++i)可以返 ...
- Bootstrap_表单_表单提示信息
平常在制作表单验证时,要提供不同的提示信息.在Bootstrap框架中也提供了这样的效果.使用了一个"help-block"样式,将提示信息以块状显示,并且显示在控件底部. < ...
- wechat-php-sdk
wechat-php-sdk 微信公众平台php版开发包 支持消息加解密方式的明文模式.兼容模式.安全模式 支持自动接入微信公众平台(步骤) 功能模块 Wechat (处理自动接入.获取与回复微信消息 ...
- KVO初探
一,概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应 ...
- SQL联合索引 与 单一列的索引
SQL联合索引 与 单一列的索引 标签: sqlwebobjectstatistics优化磁盘 2012-06-12 13:46 27992人阅读 评论(1) 收藏 举报 分类: 数据库(94) ...
- List of XML and HTML character entity references
A character entity reference refers to the content of a named entity. An entity declaration is creat ...