LeetCode469 - Convex Polygon - Medium (Python)

Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition).

[[0,0],[0,1],[1,1],[1,0]]

Answer: True
[[0,0],[0,10],[10,10],[10,0],[5,5]]

Answer: False

思路:这题问的是给一系列polygon的点,判断其是否是convex的。一个比较好的判断方法是看所有点是否是按照同一个顺序排列的,比如所有点都是依次顺时针,逆时针或者是同一条线排列。这里判断方向的方法,用到了LeetCode上有人po的 Robert Sedgewick‘s Algorithm Course里的内容。判断三个点是否按照某一个方向排列,我们可以先计算(b[0]-a[0])*(c[1]-a[1]) - (b[1]-a[1])*(c[0]-a[0])。 如果这个值大于0,那么意味着是逆时针的,如果小于0,那么是顺时针。如果等于0,那么意味着是同一条线上排列的。所以我们可以依次对points中的每三个点进行上述公式的计算,以及保存之前三个点的方向和此时三个点的方向,如果出现不一致,那么我们可以直接return False。需要注意的是这个计算也是从i=0开始的,当i=0的时候,判断的点刚好是points[-2], points[-1], points[0]. 这题的难点在于如何判断Convex。
class Solution:
    def isConvex(self, points: List[List[int]]) -> bool:
        def direction(a,b,c):
            return (b[0]-a[0])*(c[1]-a[1]) - (b[1]-a[1])*(c[0]-a[0])
        d = None
        for i in range(len(points)):
            a = direction(points[i-2],points[i-1],points[i])
            if a == 0: continue
            if d == None: d = a
            else:
                if a*d < 0: return False

        return True

原文地址:https://www.cnblogs.com/sky37/p/12244380.html

时间: 2024-08-10 13:53:57

LeetCode469 - Convex Polygon - Medium (Python)的相关文章

[LeetCode] 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

hunnu11562:The Triangle Division of the Convex Polygon(第n个卡特兰数取模)

Problem description   A convex polygon with n edges can be divided into several triangles by some non-intersect diagonals. We denote d(n) is the number of the different ways to divide the convex polygon. For example,when n is 6,there are 14 different

Leetcode: 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

HOJ 13101 The Triangle Division of the Convex Polygon(数论求卡特兰数(模不为素数))

The Triangle Division of the Convex Polygon 题意:求 n 凸多边形可以有多少种方法分解成不相交的三角形,最后值模 m. 思路:卡特兰数的例子,只是模 m 让人头疼,因为 m 不一定是素数,所以不一定存在逆元. 解法:式子为f(n) =  ( C( 2*(n-2),  (n-2) ) / (n-1))   % m :令 p = n-2, 式子可化为:f(p) = ((2*p)! / ( p! * (p+1)! ) ) % m; 对 s!分解质因素,统计个

338. Counting Bits [medium] (Python)

题目链接 https://leetcode.com/problems/counting-bits/ 题目原文 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example: For num = 5

347. Top K Frequent Elements [medium] (Python)

题目链接 https://leetcode.com/problems/top-k-frequent-elements/ 题目原文 Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ numbe

136. Single Number [medium] (Python)

题目链接 https://leetcode.com/problems/single-number/ 题目原文 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using ex

HUNAN 11562 The Triangle Division of the Convex Polygon(大卡特兰数)

http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11562&courseid=0 求n边形分解成三角形的方案数. 就是求n-2个卡特兰数,从大神那盗取了一份模板,效率极高.同时也很复杂. 1 #include <cstdio> 2 #include <cmath> 3 #include <stdlib.h> 4 #include <memory.h> 5 type

260. Single Number III [medium] (Python)

题目链接 https://leetcode.com/problems/single-number-iii/ 题目原文 Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given