题目地址: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:

  1. There are at least 3 and at most 10,000 points.
  2. Coordinates are in the range -10,000 to 10,000.
  3. 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++)的更多相关文章

  1. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  2. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  3. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  4. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  5. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  6. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  7. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. 用Rsync实现windows下同步linux服务器的数据

    一:环境 1.服务端:Red Hat Enterprise Linux Server release 6.4 (Santiago) 2.客户端:windows7旗舰版64位 3.同步对象:测试数据 4 ...

  2. Oracle-where exists()、not exists() 、in()、not in()用法以及效率差异

    0.exists() 用法: select * from T1 where exists(select 1 from T2 where T1.a=T2.a) 其中 "select 1 fro ...

  3. 非标准的xml解析器的C++实现:二、解析器的基本构造:语法表

    解析器的目的:一次从头到尾的文本遍历,文本数据 转换为 xml节点数据. 这其实是全世界所有编程语言编译或者转换为虚拟代码的基础,学会这种方法,发明一种编程语言其实只是时间问题,当然了,时间也是世界上 ...

  4. python 封装、绑定

    目录 python 封装.绑定 1.数据.方法的封装 2.隐藏属性 3.开放接口 4.绑定方法 1.对象的绑定 2.类的绑定(classmethod) 3.非绑定方法(staticmethod) 4. ...

  5. JSP内置对象之out对象

    一.       JSP内置对象的概述     由于JSP使用java作为脚本语言,所以JSP将具有强大的对象处理能力,并且可以动态地创建Web页面内容.但Java语法在使用一个对象前,需要先实例化这 ...

  6. Hadoop入门 运行环境搭建

    模板虚拟机 目录 模板虚拟机 1 硬件 2 操作系统 3 IP地址和主机名称 vm windows10 Hadoop100服务器 远程访问工具 其他准备 克隆虚拟机 克隆 修改主机名/ip 安装jdk ...

  7. 【2021赣网杯web(一)】gwb-web-easypop

    源码分析 <?php error_reporting(0); highlight_file(__FILE__); $pwd=getcwd(); class func { public $mod1 ...

  8. listView 多布局

    最近在开发项目中遇到了实现类似淘宝首页的需求,使用listView可以解决,在此记录一下. 实现步骤: 重写 getViewTypeCount() – 返回你有多少个不同的布局 重写 getItemV ...

  9. Siebel调用WebService

    Siebel可以调用外部系统的接口,通过WebService的接入方式实现,所在的项目都是通过ESB,其他系统的接口都要经过ESB,由ESB提供WSDL文档,通过Siebel调用. 一.修改Tools ...

  10. Output of C++ Program | Set 2

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 using namespace st ...