# 356 Line Reflection

## 356. [Line Reflection](https://leetcode.com/problems/line-reflection/description/)

## 1. Question

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:**

Givenpoints=`[[1,1],[-1,1]]`, return`true`.

**Example 2:**

Givenpoints=`[[1,1],[-1,-1]]`, return`false`.

**Follow up:**\
Could you do better than O(n2)?

## 2. Implementation

思路:

1. 如果存在一条线评分所有点，那么一个点和它关于这条线对称的点的x(横坐标值)和y(纵坐标)的值的和是一样的，包括最大和最小值，由于要求的是这个平分点的线是垂直于x轴的，所以我们只关心点的x值即可
2. 找出所有点中x值最大和最小的值，得到它们的和sum
3. 将每个点都以 x + "#" + y的形式计算它们的hashcode放入set里
4. 再一次遍历所有点，如果当前的点所对应的点 sum - x + "#" + y不在set里，说明不存在一条平分所有点的线

**(1) HashTable**

```java
class Solution {
    public boolean isReflected(int[][] points) {
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        Set<String> set = new HashSet<>();

        for (int[] point : points) {
            max = Math.max(max, point[0]);
            min = Math.min(min, point[0]);
            String code = point[0] + "#" + point[1];
            set.add(code);
        }

        int sum = min + max;
        for (int[] point : points) {
            String code = (sum - point[0]) + "#" + point[1];
            if (!set.contains(code)) {
                return false;
            }
        }
        return true;
    }
}
```

## 3. Time & Space Complexity

**HashTable:** 时间复杂度O(n), 空间复杂度O(n)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://protegejj.gitbook.io/algorithm-practice/google/356-line-reflection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
