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

 class Solution {
public:
bool isReflected(vector<pair<int, int>>& points) {
if (points.size() == ) return true;
unordered_set<string> p;
int minX = INT_MAX, maxX = INT_MIN;
for (int i = ; i < points.size(); i++) {
int x = points[i].first, y = points[i].second;
minX = std::min(minX, x);
maxX = std::max(maxX, x);
string code = std::to_string(x) + "|" + std::to_string(y);
p.insert(code);
}
int sum = minX + maxX;
for (int i = ; i < points.size(); i++) {
int x = sum - points[i].first, y = points[i].second;
string code = std::to_string(x) + "|" + std::to_string(y);
if (p.count(code) == ) return false;
}
return true;
}
};

Line Reflection -- LeetCode的更多相关文章

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

  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. lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II

    变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...

  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. hdu5828 Rikka with Sequence

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5828 [题解] 考虑bzoj3211 花神游历各国,只是多了区间加操作. 考虑上题写法,区间全为1打标记 ...

  2. PHP正则 贪婪匹配与非贪婪匹配

    $str = ".abcdeabcde"; preg_match('/a.+?e/', $str, $match); print_r($match); Array ( [0] =& ...

  3. [IOS]VMware上虚拟机MAC安装XCode

    1:VMware上虚拟机MAC安装前 VMware上安装Xcode之后 2:安装Xcode过程:把Xcode复制到虚拟机桌面上 3:复制完成之后,双击Xcode_6.4.dmg 文件 4:把Xcode ...

  4. Spring的BeanFactory体系结构(一)

    本文使用的代码是: Spring 3.0 接 触Spring也有很长一段时间了.但是,每次都是直接使用Spring直接提供的API,时间久了,自然也会想探索Spring里面的奥秘.今天上 午,整理出了 ...

  5. Linux简介——(一)

    1. 常见操作系统 - 服务端操作系统 : linux.unix.windows server - 单机操作系统 : windows(dos .ucdos.win95.win98.win2000.xp ...

  6. LCD实验学习笔记(九):UART

    s3c2440包含三个通用异步收发器,可工作于中断模式或DMA模式.每个UART包含两个64字节的FIFOs用于接收和发送数据.可编程设置波特率.1或2个停止位,5/6/7/8个数据位和奇偶校验状态. ...

  7. OWASP SSL 高级审查工具

    http://www.linuxidc.com/Linux/2016-03/129164.htm InfoWorld 在部署.运营和保障网络安全领域精选出了年度开源工具获奖者. 最佳开源网络和安全软件 ...

  8. ZOJ-3430

    Detect the Virus  Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his c ...

  9. AC日记——红色的幻想乡 洛谷 P3801

    红色的幻想乡 思路: 线段树+容斥原理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #de ...

  10. AC日记——The Shortest Path in Nya Graph hdu 4725

    4725 思路: 拆点建图跑最短路: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...