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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. 356. Line Reflection

    首先找到X方向的中点,如果中点是一个点,那么分别从这个点开始往左右找就行:如果是一个区间,比如1 2之间,那么首先总点数得是偶数,然后以1和2往左右两边找就行.. 找的时候,有3种情况: 同时没找到, ...

  5. [LeetCode] Mirror Reflection 镜面反射

    There is a special square room with mirrors on each of the four walls.  Except for the southwest cor ...

  6. [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. ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. NSDateFormatter 相关理解

    Formatter译为格式,相应的NSDateFormatter就相当于是NSDate的转换类,将NSDate转换为另一种格式,或转换回来.NSDate没有自己的输出,需要借助NSDateFormat ...

  2. 最新版 CocoaPods 的安装流程

                                       iOS 最新版 CocoaPods 的安装流程     1.移除现有Ruby默认源 $gem sources --remove h ...

  3. 为什么<b></b>不推荐使用

    曾经在网上看见说:不推荐是用b标签,咦,我好像用过不少,难道我又坑了别人……度娘是这样说的:只要是从网页的简洁性和搜索引擎的友好度来看的.<b>是加粗,和css的font-weight在视 ...

  4. php_codesninffer phpcs用法学习使用:

    18:34 2016/1/12php_codesninffer phpcs用法学习使用:可以加一个参数设置结果输出为各种格式:如source格式:$ phpcs -s --report=source ...

  5. apache配置rewrite及.htaccess文件(转载)

    今天看到一个哥们的帖子发了个rewrite的帖子,以前也写过一个,配置挺简单的,但当时没注意这个问题,当时没有用到.htaccess文件,在机子上测试了一下,发现确实没法用,于是开始找问题的所在. 自 ...

  6. 学习笔记_springmvc返回值、数据写到页面、表单提交、ajax、重定向

    数据写到页面 后台往前台传数据 TestController添加 /** * 方法的返回值采用ModelAndView, new ModelAndView("index", map ...

  7. mysql 使用存储过程批量插数据

    #创建测试表 DROP TABLE IF EXISTS test.test; CREATE TABLE test.test( id int(10) not null auto_increment, a ...

  8. 高性能Server---Reactor模型

    原文地址:http://www.ivaneye.com/2016/07/23/iomodel.html 无处不在的C/S架构 在这个充斥着云的时代,我们使用的软件可以说99%都是C/S架构的! 你发邮 ...

  9. TID大会学习心得之软技能

    软技能(Personal development as a software developer) John Sonmez : Simple programmer的创始人 2.1 学习方法 学习不是一 ...

  10. nginx配置之取消index.php同时不影响js,css功能

    server { listen 8084; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; ...