356. 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:
Input: [[1,1],[-1,1]]
Output: true
Example 2:
Input: [[1,1],[-1,-1]]
Output: false
Follow up: Could you do better than O(n2) ?
解题要点:
用set来记录原始point,然后把x轴里最大最小数相加后,再遍历一遍,新的x用相加后的数字减去x,看这个数在不在set里,如不在,返回False。
class Solution(object):
def isReflected(self, points):
"""
:type points: List[List[int]]
:rtype: bool
"""
pointSet = set()
maxNum = -sys.maxint
minNum = sys.maxint
for x, y in points:
maxNum = max(maxNum, x)
minNum = min(minNum, x)
pointSet.add((x, y))
sumNum = maxNum + minNum
for x, y in points:
tempTuple = (sumNum - x, y)
if tempTuple not in pointSet:
return False
return True
Last updated
Was this helpful?