【LeetCode】469. Convex Polygon 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/convex-polygon/
题目描述
Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition).
Note:
- There are at least 3 and at most 10,000 points.
- Coordinates are in the range -10,000 to 10,000.
- You may assume the polygon formed by given points is always a simple polygon (Simple polygon definition). In other words, we ensure that exactly two edges intersect at each vertex, and that edges otherwise don’t intersect each other.
Example 1:
[[0,0],[0,1],[1,1],[1,0]]
Answer: True
Explanation:
Example 2:
[[0,0],[0,10],[10,10],[10,0],[5,5]]
Answer: False
Explanation:
题目大意
判断一个多边形是不是凸多边形。
解题方法
计算向量夹角
借鉴了参考资料中的做法:
假设多边形的顶点是呈逆时针排列的,多边形是凸多边形的充要条件是:对于多边形的任何一条边,其下一条边必须是不朝右拐的(可以向左拐,也可以不拐)。那么如何判断下一条边是不朝右拐呢?假设假设当前边形成的向量是v1,下一条边形成的向量是v2,那么v2不朝右拐的充要条件是v1 x v2 >= 0,也就是它们形成的有向三角形的面积大于等于0,符合右手法则。
C++代码如下:
class Solution {
public:
bool isConvex(vector<vector<int>>& points) {
int N = points.size();
long pre = 0;
for (int i = 0; i < N; ++i) {
long cur = angle({points[i], points[(i + 1) % N], points[(i + 2) % N]});
if (cur != 0) {
if (cur * pre < 0)
return false;
else
pre = cur;
}
}
return true;
}
int angle(vector<vector<int>> A) {
return (A[1][0] - A[0][0]) * (A[2][1] - A[0][1]) -
(A[1][1] - A[0][1]) * (A[2][0] - A[0][0]);
}
};
参考资料:https://blog.csdn.net/magicbean2/article/details/78593338
日期
2019 年 9 月 20 日 —— 是选择中国互联网式加班?还是外企式养生?
【LeetCode】469. Convex Polygon 解题报告(C++)的更多相关文章
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
随机推荐
- mount 挂载详解
挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式:mount [-t vfstype] [-o option ...
- python的包与模块
win +R d
- GWAS在农业上应用
农业的组学技术应用虽然落后于人的研究,这是什么意义的问题,但有时农业基因组有自己无可比拟的优势,那就是材料.下面介绍GWAS应用. GWAS(Genome-wide association study ...
- 问题记录:SNP 标记 phasing
GATK4 检测的SNP标记,有些位点会在检测过程中完成 phasing,在后续做基因型填充的时候有坑. GATK4 phasing 结果的缺失位点不是 ./. 也不是 .|. 而是直接变成一个单独 ...
- mysql_sql查性能语句
mysql> SHOW PROCESSLIST; +----+--------+----------------------+-------+-------------+--------+--- ...
- 基于 芯片 nordic 52832 rtt 调试(Mac 电脑)
代码配置 // <e> NRF_LOG_BACKEND_SERIAL_USES_UART - If enabled data is printed over UART //======== ...
- Perl if条件判断
Perl 条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 条件判断常用: True #布尔值 not True #布尔值 ! True ...
- JVM1 JVM与Java体系结构
目录 JVM与Java体系结构 虚拟机与Java虚拟机 虚拟机 Java虚拟机 JVM的位置 JVM的整体结构 Java代码执行流程 JVM的架构模型 基于栈的指令级架构 基于寄存器的指令级架构 两种 ...
- 零基础学习java------day15--------collections用法,比较器,Set(TreeSet,TreeMap),异常
1. Collections用法 Collections: 集合的工具类public static <T> void sort(List<T> list) 排序,升序publi ...
- winxp 关闭445端口
关闭445端口的方法方法很多,但是我比较推荐以下这种方法: 修改注册表,添加一个键值 Hive: HKEY_LOCAL_MACHINE Key: System\Controlset\Services\ ...