[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. 这道题说是在笛卡尔 ...
随机推荐
- oracle操作符
Oracle中算术操作符(+)(-)(*)(/) 值得注意的是:/ 在oracle中就相当于显示中的除法 5/2 = 2.5 比较操作符: 其中等号可以换成其他运算符:(后面为该操作符的单条件查询样例 ...
- TypeScript 强类型 JavaScript – Rafy Web 框架选型
今天看到了 AngularJs 2.0 版本将基于 TypeScript 构建 的消息.与同事们对 TypeScript 展开了讨论.本文记录一些个人的想法. 理想的 JavaScript 开发模式 ...
- iOS 原生HTTP POST请求上传图片
今天项目里做一个上传图片等个人信息的时候,使用了第三方AFNetworking - (AFHTTPRequestOperation *)POST:(NSString *)URLString param ...
- 用JWT来保护我们的ASP.NET Core Web API
在上一篇博客中,自己动手写了一个Middleware来处理API的授权验证,现在就采用另外一种方式来处理这个授权验证的问题,毕竟现在也 有不少开源的东西可以用,今天用的是JWT. 什么是JWT呢?JW ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
- Debian的软件包管理工具命令 (dpkg,apt-get)详解
本文转载于:http://blog.chinaunix.net/uid-20769502-id-106056.html 1.dpkg包管理工具 dpkg --info "软件包名&quo ...
- Atitit 自动化gui 与 发帖机 技术
Atitit 自动化gui 与 发帖机 技术 1.1. Gui tech1 1.2. 自动化软件测试1 1.3. selenium attilax1 1.4. 图形脚本语言Sikuli1 1.5. ...
- Java微信公众平台接口封装源码分享
前言: 这篇博客是在三月初动手项目的时候准备写的,但是为了完成项目只好拖延时间写这篇博客,顺便也可以在项目中应用我自己总结的的一些经验.今天看来,这些方法的应用还是可以的,至少实现了我之前的 ...
- 设计模式-策略模式(Strategy Model)
1.概述 在开发过程中常常会遇到类似问题,实现一个功能的时候往往有多种算法/方法(策略),我们可以根据环境的不同来使用不同的算法或策略来实现这一功能. 如在人物比较排序的实现中,我们有 ...
- JavaScript中数据类型转换总结
JavaScript中数据类型转换总结 在js中,数据类型转换分为显式数据类型转换和隐式数据类型转换. 1, 显式数据类型转换 a:转数字: 1)Number转换: 代码: var a = " ...