?LeetCode刷題實(shí)戰(zhàn)469:凸多邊形
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.
示例? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
[[0,0],[0,1],[1,1],[1,0]]
輸出:True

[[0,0],[0,10],[10,10],[10,0],[5,5]]
輸出:False
解釋:

解題
class?Solution:
????def?isConvex(self, points: List[List[int]])?-> bool:
????????def?cal_cross_product(A, B, C):
????????????AB = [B[0] - A[0], B[1] - A[1]]
????????????AC = [C[0] - A[0], C[1] - A[1]]
????????????return?AB[0] * AC[1] - AB[1] * AC[0]
????????flag = 0
????????n = len(points)
????????for?i in?range(n):
????????????# cur > 0 表示points是按逆時(shí)針輸出的;cur < 0,順時(shí)針
????????????cur = cal_cross_product(points[i], points[(i + 1) % n], points[(i + 2) % n])
????????????if?cur != 0:
????????????????# 說明異號(hào), 說明有個(gè)角大于180度
????????????????if?cur * flag < 0:
????????????????????return?False
????????????????else:
????????????????????flag = cur
????????return?True
LeetCode1-460題匯總,希望對(duì)你有點(diǎn)幫助!
LeetCode刷題實(shí)戰(zhàn)461:漢明距離
LeetCode刷題實(shí)戰(zhàn)462:最少移動(dòng)次數(shù)使數(shù)組元素相等 II
LeetCode刷題實(shí)戰(zhàn)463:島嶼的周長(zhǎng)
LeetCode刷題實(shí)戰(zhàn)464:我能贏嗎
LeetCode刷題實(shí)戰(zhàn)465:最優(yōu)賬單平衡
LeetCode刷題實(shí)戰(zhàn)466:統(tǒng)計(jì)重復(fù)個(gè)數(shù)
LeetCode刷題實(shí)戰(zhàn)467:環(huán)繞字符串中唯一的子字符串
LeetCode刷題實(shí)戰(zhàn)468:驗(yàn)證IP地址
