469. Convex Polygon
来源:互联网 发布:图片怎么上传到淘宝网 编辑:程序博客网 时间:2024/06/07 15:29
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: TrueExplanation:
Example 2:
[[0,0],[0,10],[10,10],[10,0],[5,5]]Answer: FalseExplanation:这道题开始没有思路,看了这篇博客:Orientation of 3 ordered points。这篇博客里用的是p1p2,p2p3的组合,如果出现全是直角的情况,(y2 - y1)*(x3 - x2) - (y3 - y2)*(x2 - x1)会恒等于0导致算法失败。所以在我们的算法里,用p1p2,p1p3的组合,这种组合只有在p1p2p3成一条直线的时候才会等于0,但是题目中说了不会给这样的情况,给的店必然能组成一个多边形。还有一点要注意,如果用前后状态相乘是否小于0来判断前后状态是不是相反的,要防止curr * last < 0溢出。代码如下:
public class Solution { public boolean isConvex(List<List<Integer>> points) { int len = points.size(); int last = 0, curr = 0; for (int i = 2; i < len + 3; i ++) { List<Integer> point1 = points.get((i - 2) % len); List<Integer> point2 = points.get((i - 1) % len); List<Integer> point3 = points.get((i) % len); curr = (point2.get(1) - point1.get(1)) * (point3.get(0) - point1.get(0)) - (point3.get(1) - point1.get(1)) * (point2.get(0) - point1.get(0)); //System.out.println(curr); if (curr != 0) { curr = curr / Math.abs(curr); //prevent overflow if (curr * last < 0) { return false; } last = curr; } } return true; }}
0 0
- 469. Convex Polygon
- 469. Convex Polygon
- 469. Convex Polygon
- [Leetcode] 469. Convex Polygon 解题报告
- hdu - 4159 - Regular Convex Polygon - 精度问题
- HDU 4195 Regular Convex Polygon(正多边形)
- The Triangle Division of the Convex Polygon
- POJ-4042(简单几何)(Regular Convex Polygon )
- hdu4195Regular Convex Polygon poj2242The Circumference of the Circle
- convex
- 正好用到求凸包(Convex Polygon)的问题,就把他记下来
- hdoj 4195 Regular Convex Polygon(余弦定理+正凸多边形性质)
- hunnu11562:The Triangle Division of the Convex Polygon(第n个卡特兰数取模)
- polygon
- Polygon
- HNU 13101 The Triangle Division of the Convex Polygon 卡特兰数第n项%m(m可为非素数
- convex hull
- Convex combination
- Android RecyclerView 的那些开源 LayoutManager
- CKplayer纯净播放器设置示例(可不显示广告)
- 蓝牙耳机录音
- PE文件
- mysql无法开启:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql'
- 469. Convex Polygon
- ${name}用法
- Unity+PHP+MySQ实现用户登录注册
- 病毒侵袭持续中-AC自动机
- 网络——IP协议
- 537. Complex Number Multiplication
- spark源码分析-源码编译(1)
- http和https区别
- ORACLE 创建视图时,提示用户权限不足