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 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.
本题精妙之处在于:1. 如何最快找到possible的line的x axis(我最开始想到要用quickselect find median的方法,结果别人有min max方法)
2. 如何最方便确定一个点关于该line的reflection是否存在,由于既有x又有y,不太好处理,别人有个聪明的办法把x跟y组合成string,然后用hashset
学到了:以后处理多个value共同作用的时候,可以考虑wrapper class(但是不好用set), 更应该想一想能不能直接把它们组合成string(直接又好利用set来find)
public class Solution {
public boolean isReflected(int[][] points) {
int minX = Integer.MAX_VALUE, maxX = Integer.MIN_VALUE;
HashSet<String> set = new HashSet<>();
for (int[] point : points) {
minX = Math.min(minX, point[0]);
maxX = Math.max(maxX, point[0]);
set.add(point[0] + "a" + point[1]);
}
double sum = minX + maxX;
for (int[] point : points) {
if (!set.contains((int)(sum-point[0]) + "a" + point[1]))
return false;
}
return true;
}
}
Leetcode: 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 ...
- 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 ...
- [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 ...
- 356. Line Reflection
首先找到X方向的中点,如果中点是一个点,那么分别从这个点开始往左右找就行:如果是一个区间,比如1 2之间,那么首先总点数得是偶数,然后以1和2往左右两边找就行.. 找的时候,有3种情况: 同时没找到, ...
- [LeetCode] Mirror Reflection 镜面反射
There is a special square room with mirrors on each of the four walls. Except for the southwest cor ...
- [LeetCode] Max Points on a Line 共线点个数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- android 开发项目笔记1
1.xml文件中@string/name @+id/name @id/name 的用法与区别: @string/name 一般长用于从别的资源中获取键值对 @+id/name 为控件指定名 ...
- MongoDB 3.X 用户权限控制
摘要: MongoDB 3.0 安全权限访问控制,在添加用户上面3.0版本和之前的版本有很大的区别,这里就说明下3.0的添加用户的方法. 环境.测试: 在安装MongoDB之后,先关闭auth认证,进 ...
- Android课程---关于GridView网格视图的学习
activity_ui6.xml <?xml version="1.0" encoding="utf-8"?> <GridView xmlns ...
- hdu 4899 Hero meet devil
传送阵:http://acm.hdu.edu.cn/showproblem.php?pid=4899 题目大意:给定一个DNA序列,求有多少长度为m的序列与该序列的最长公共子序列长度为0,1...|S ...
- 【emWin】例程五:显示数值
实验指导书及代码包下载: 链接:http://pan.baidu.com/s/1pLexsAf密码:p0jf 实验现象:
- 数据库使用数据泵迁移遇到LOB字段
impdp system/Clic1234 attach=SYS_IMPORT_ILEARN_TRA desc ILEARN_TRA.NOTIFI_TACTIC desc ILEARN_TRA.MSG ...
- 清理session的案例
and OPNAME ='Sort Output'; and OPNAME ='Sort Output'; and OPNAME ='Sort Output' ); select 'alter sys ...
- mac安装tensorflow报错
问题:mac安装tensorflow过程中,爆出oserror:permission denied 解决方案:关闭mac的sip,然后sudo安装 关闭sip的方法:重启mac,按住command+R ...
- MyBatis学习总结(五)——实现关联表查询(转载)
本文转载自:http://www.cnblogs.com/jpf-java/p/6013516.html 一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数 ...
- Tomcat 随机挂掉
最近遇到一些诡异的事情,1Apache + 4Tomcat的系统中,每天偶尔会发现其中的一台或几台Tomcat宕机了. 今天特别频繁,宕了有6次之多. 环境: Windows Server 20 ...