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 ...
随机推荐
- JavaScript Window - 浏览器对象模型
浏览器对象模型 (BOM) 使 JavaScript 有能力与浏览器"对话". 浏览器对象模型 (BOM) 浏览器对象模型(Browser Object Model (BOM))尚 ...
- Spring 创建bean的模式
在默认情况下,spring创建bean是单例模式 scope="singleton ",还有一种方式为多例模式[prototype] scope sing ...
- 优秀Android开源项目
开源项目汇总: Trinea/android-open-project · GitHub 包含个性化控件.工具库.优秀项目.开发及测试工具等 优秀完整项目: 1.Google I/O Android ...
- html中的空格可以用什么代替
半角空格用 代替,全角的空格可以直接在网页里生效. 打全角空格的两种方法:1.智能ABC按v1,选择第一个2.按shift+空格切换输入法的“半.全角”状态为全角再按空格
- 单选按钮 点击value值自动把单选按钮选中
HTML 代码 <tr> <td align="right">性别:</td> <td><inputt ...
- 全部与精简切换显示jQuery实例教程
下面是某网站上的一个品牌列表展示效果,用户进入页面时,品牌列表默认是精简显示的(即不完整的品牌列表)效果如下图所示: 用户可以单击商品列表下方的“显示全部品牌”按钮来显示全部的品牌.单击“显示全部品牌 ...
- 自定义Excel导出简易组件
1.组件原理 excel的数据存储是以xml格式存储的,所以导出Excel文件可以通过生成XML来实现.当然XML必须符合一定的格式要求. 2.组件实现 (1)新建类库文件“MyExcel” (2)添 ...
- YII 创建后台模块
1,在protected/config/main.php目录下修改如下目录 'modules'=>array( // uncomment the following to enable the ...
- Android扫描二维码 实现 登录网页
工程代码:ScanQRcode.zip ------------------------------------------------------------------ 1. 扫描二维码登录的实现 ...
- 前端html+css之第十四天
一.HTML 1.HTML是什么? Hypertext Markup Language, 中文也就是超文本链接标示语言. HTML是一套规则,一套浏览器认识的规则. 2.开发者: (1)学习Html规 ...