469 Convex Polygon
Last updated
Last updated
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:
Example 2:
(1) Geometry
思路: 凸多边形的定义是每个顶点的角度都小于180度, 判断一个多边形
要注意的地方有两点:(1)如果cross product是0,我们应该忽略,否则会影响之后的结果. (2)由于数字可能溢出,所以curCroosProduct和preCrossProduct要用long
时间复杂度O(n), n是顶点的个数, 空间复杂度O(1)