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 ...
随机推荐
- animationWithKeyPath键值对
animationWithKeyPath键值对的方式来改变动画 <Jacky Shin:可以从这个网址查到哪些可以做为动画效果, 打开xcode帮助,搜索animatable propertie ...
- 不能错过的XCode插件
以便自己记忆网上找的! 以下是搜集的一些有力的XCode插件. 全能搜索家CodePilot 2.0 你要找的是文件?是文件夹?是代码?Never Mind,CMD+SHIFT+X调出CodePilo ...
- .net版ckeditor配置水印功能(转)
本文简单讲解ckfinder控件给上图片加水印效果. 1.将ckfinder/plugins/watermark/bin/Debug目录下的CKFinder_Watermark.dll和CKFinde ...
- C# div布局
本文讲解使用DIV+CSS布局最基本的内容,读完本文你讲会使用DIV+CSS进行简单的页面布局. 转载请标明:http://www.kwstu.com/ArticleView/divcss_20139 ...
- POJ 1734.Sightseeing trip (Floyd 最小环)
Floyd 最小环模板题 code /* floyd最小环,记录路径,时间复杂度O(n^3) 不能处理负环 */ #include <iostream> #include <cstr ...
- 一个基于nodejs,支持http/https的中间人(MITM)代理,便于渗透测试和开发调试。
源码地址:https://github.com/wuchangming/node-mitmproxy node-mitmproxy node-mitmproxy是一个基于nodejs,支持http/h ...
- 那些年被我坑过的Python——第十章Broker(rabbitMQ/redis)
基于RabbitMQ的direct任务驱动异步RPC程序实现: RPC_dispatcher指令分发器: #!/usr/bin/env python # -*- coding:utf-8 -*- __ ...
- UFLDL实验报告3:Self-taught
Self-taught 自我学习器实验报告 1.Self-taught 自我学习实验描述 自我学习是无监督特征学习算法,自我学习意味着算法能够从未标注数据中学习,从而使机器学习算法能够获得更大数量的数 ...
- MSSQL中datetime与unix时间戳互转
//ms sql datetime 转unix时间戳 SELECT DATEDIFF(s, '19700101',GETDATE()) //ms sql unix时间戳 转datetime 涉及到时区 ...
- 在iOS7中修改状态栏字体的颜色-b
状态栏的字体为黑色: UIStatusBarStyleDefault 状态栏的字体为白色: UIStatusBarStyleLightContent 一.在 info.plist 中,将 View ...