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)?
解法1:O(N) Time + O(N) Space
这题的题意是对于N个给定的点,找一下是否存在一条平行与y轴的线,使得所有点都对称与此条线。
那么怎么确定这条平行的线是关键。
如果点都是对称存在的,那么最左面的点和最右面的点当中的线就是平行于y轴的对称线。所有其他的点都必须以此为基准。
用一个set存储每一个点,并且找出最左最右两个点。找出以后,对于每一个点,检查他的对称点是否在set中。
放入set的时候用到了Arrays.hashCode(int[])的function。
C++
Java