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 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)?

思路:假设如果存在这样的一条对称轴,那么对于一对对称点,它们的X坐标之和应该是个定值。

我们找到所有点中X坐标的最小值和最大值,两者之和就是这个定值。

然后将所有点的坐标记录在set中,然后判断每个点的对称点是否在set中。

时间复杂度O(N)。

 1 class Solution {
 2 public:
 3     bool isReflected(vector<pair<int, int>>& points) {
 4         if (points.size() == 0) return true;
 5         unordered_set<string> p;
 6         int minX = INT_MAX, maxX = INT_MIN;
 7         for (int i = 0; i < points.size(); i++) {
 8             int x = points[i].first, y = points[i].second;
 9             minX = std::min(minX, x);
10             maxX = std::max(maxX, x);
11             string code = std::to_string(x) + "|" + std::to_string(y);
12             p.insert(code);
13         }
14         int sum = minX + maxX;
15         for (int i = 0; i < points.size(); i++) {
16             int x = sum - points[i].first, y = points[i].second;
17             string code = std::to_string(x) + "|" + std::to_string(y);
18             if (p.count(code) == 0) return false;
19         }
20         return true;
21     }
22 };
时间: 2024-10-08 19:54:37

Line Reflection -- LeetCode的相关文章

[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 set of 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 d

LeetCode &quot;Line Reflection&quot;

First you find the Y, and then do a pairing game - hashtable is good. class Solution { public: bool isReflected(vector<pair<int, int>>& points) { int n = points.size(); if (n < 2) return true; unordered_map<int, unordered_map<int,

[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 set of 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

LeetCode: Text Justification [068]

[题目] Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. P

leetcode 118. Pascal&#39;s Triangle &amp;&amp; leetcode 119. Pascal&#39;s Triangle II

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]nextline[j] = currentline[j] + currentline[j+1] 1 vector<vector<int> > generate(int numRows) 2

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,

【LeetCode】哈希表 hash_table(共88题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target . 题解:我这次最大范围的优化代码, hash-table + one pass,时间复杂度 O(N),空间复杂度 O(N).重点在于动态找,一边生成hash-tabl