[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 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) ?
Hint:
- Find the smallest and largest x-value for all points.
- If there is a line then it should be at y = (minX + maxX) / 2.
- For each point, make sure that it has a reflected point in the opposite side.
Credits:
Special thanks to @memoryless for adding this problem and creating all test cases.
这道题给了我们一堆点,问我们存不存在一条平行于y轴的直线,使得所有的点关于该直线对称。题目中的提示给的相当充分,我们只要按照提示的步骤来做就可以解题了。首先我们找到所有点的横坐标的最大值和最小值,那么二者的平均值就是中间直线的横坐标,然后我们遍历每个点,如果都能找到直线对称的另一个点,则返回true,反之返回false,参见代码如下:
解法一:
class Solution {
public:
bool isReflected(vector<pair<int, int>>& points) {
unordered_map<int, set<int>> m;
int mx = INT_MIN, mn = INT_MAX;
for (auto a : points) {
mx = max(mx, a.first);
mn = min(mn, a.first);
m[a.first].insert(a.second);
}
double y = (double)(mx + mn) / ;
for (auto a : points) {
int t = * y - a.first;
if (!m.count(t) || !m[t].count(a.second)) {
return false;
}
}
return true;
}
};
下面这种解法没有求最大值和最小值,而是把所有的横坐标累加起来,然后求平均数,基本思路都相同,参见代码如下:
解法二:
class Solution {
public:
bool isReflected(vector<pair<int, int>>& points) {
if (points.empty()) return true;
set<pair<int, int>> pts;
double y = ;
for (auto a : points) {
pts.insert(a);
y += a.first;
}
y /= points.size();
for (auto a : pts) {
if (!pts.count({y * - a.first, a.second})) {
return false;
}
}
return true;
}
};
类似题目:
参考资料:
https://leetcode.com/problems/line-reflection/description/
https://leetcode.com/discuss/107661/48-ms-short-c-solution
https://leetcode.com/discuss/107761/group-by-y-then-sort-by-x-17ms
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Line Reflection 直线对称的更多相关文章
- 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 ...
- HDU 2671 Can't be easier(数学题,点关于直线对称)
题目 //数学题//直线 y = k * x + b//直线 ax+by+c=0; 点 (x0,y0); 点到直线距离 d = (ax0+by0+c)/sqrt(a^2+b^2) /********* ...
- LeetCode 5 最长对称串
LeetCode 5 最长对称串 最早时候做这道题的时候还是用Java写的,用的是字符串匹配的思路,一直Time Limit Exceeded.甚至还想过用KMP开优化子串查找. public cla ...
- Leetcode:面试题28. 对称的二叉树
Leetcode:面试题28. 对称的二叉树 Leetcode:面试题28. 对称的二叉树 Talk is cheap . Show me the code . /** * Definition fo ...
- [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 ...
- 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 ...
- [LeetCode] Strobogrammatic Number III 对称数之三
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] Strobogrammatic Number II 对称数之二
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [CareerCup] 7.3 Line Intersection 直线相交
7.3 Given two lines on a Cartesian plane, determine whether the two lines would intersect. 这道题说是在笛卡尔 ...
随机推荐
- 细说Java主流日志工具库
概述 在项目开发中,为了跟踪代码的运行情况,常常要使用日志来记录信息. 在Java世界,有很多的日志工具库来实现日志功能,避免了我们重复造轮子. 我们先来逐一了解一下主流日志工具. java.util ...
- AngularJS中get请求URL出现跨域问题
今天早上帮助同学看了一个AngularJS的问题,主要是请求中出现了跨域访问,请求被阻止. 下面是她给我的代码: <html lang="en" ng-app="m ...
- Visual Studio 2013 Ultimate因为CodeLens功能导致Microsoft.Alm.Shared.Remoting.RemoteContainer.dll高CPU占用率的折中解决方案
1.为什么Microsoft.Alm.Shared.Remoting.RemoteContainer.dll的CPU占用率以及内存使用率会那么高? 在Visual Studio 2013 Ultima ...
- C#开发微信门户及应用(11)--微信菜单的多种表现方式介绍
在前面一系列文章中,我们可以看到微信自定义菜单的重要性,可以说微信公众号账号中,菜单是用户的第一印象,我们要规划好这些菜单的内容,布局等信息.根据微信菜单的定义,我们可以看到,一般菜单主要分为两种,一 ...
- Yii2.X 多语言-类图
- Wireshark
0. install Wireshark on Ubuntu 14 sudo apt-get install -y wireshark sudo addgroup -quiet -system wir ...
- 数据结构:堆排序 (python版) 小顶堆实现从大到小排序 | 大顶堆实现从小到大排序
#!/usr/bin/env python # -*- coding:utf-8 -*- ''' Author: Minion-Xu 小堆序实现从大到小排序,大堆序实现从小到大排序 重点的地方:小堆序 ...
- react-echarts之折线图的显示
react中想要实现折线图和饼图的功能,需要引入react-echarts包,然后再实现折线图的功能.我这里引用的版本是:0.1.1.其他的写法参echarts官网即可.下面详细讲解的是我在react ...
- SVG入门
这部分包括三个内容: 1. 基本图形 2. 基本属性 3. 基础操作API 基本图形 名称 描述 参数 图示 rect 定义一个矩形 x="矩形的左上角的x轴" y="矩 ...
- ArcGIS Engine开发之属性查询
属性查询即基于空间数据的属性数据的查询,通过用户提交SQL语言中的where语句定义的查询条件,对属性数据进行搜索,从而得到查询结果的操作. 相关的类与接口 与属性查询功能相关的类主要有QureyFi ...