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. vs调试 配置IISExpress允许局域网内部访问

    内网可访问后,本机不能使用localhost   1.找到IISExpress的配置文件,位于 <文档>/IISExpress/config文件夹下,打开applicationhost.c ...

  2. vue手势解决方案

    1.需求 因为项目中要做一个可以移动.旋转和放缩具有合成图片的功能,例如: 剑可以随意移动,然后把位移.旋转角度和放缩值传给后台进行合成. 2.解决方案 网上搜到手势插件AlloyFinger,htt ...

  3. Python3.3.3 安装(Linux系统)

    1.wget http://www.python.org/ftp/python/3.3.3/Python-3.3.3.tgz //检查http://www.python.org/ftp/python网 ...

  4. 使用ubuntun16.04代码笔记

    (1)cd  /代表到根目录下面:ls表示将盘中内容列出:cd  /home表是打开根目录下的home文件夹:(注意:凡是根目录下的文件夹前面都要加 /) (2)快捷键方式:可以用tab自动补全 (1 ...

  5. nodewebkitV0.21.6版本的学习

    1.下载 https://nwjs.io/ 建议下载sdk版本,有调试功能,正式发布时,使用normal版本 下载以后解压,得到的文件目录如下 点击nw.exe,显示如下 2.hello world ...

  6. C++之容器(关联容器)

    关联容器和顺序容器的本质区别:关联容器是通过键存取和读取元素.顺序容器通过元素在容器中的位置顺序存储和访问元素.因此,关联容器不提供front.push_front.pop_front.back.pu ...

  7. 在 kernel 下打出 有帶參數的log。 怪異現象與解決方式。

    code battery_log(BAT_LOG_CRTI, "youchihwang abc10010 xxxaaa8-2\r\n"); battery_log(BAT_LOG_ ...

  8. 64_f1

    FUR-0.4.6-13.fc26.x86_64.rpm 13-Feb-2017 23:32 45882 Falcon-0.9.6.8-11.fc26.i686.rpm 13-Feb-2017 23: ...

  9. Centos. Mac 通过nfs 搭建共享目录

    centos 关闭fiewalld,selinux yum install yum install nfs-utils portmap vim /etc/exports 文件写入时使用anonuid用 ...

  10. 2017多校第5场 HDU 6085 Rikka with Candies bitset

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6085 题意:存在两个长度为n,m的数组A,B.有q个询问,每个询问有一个数字k,可以得到Ai%Bj=k ...