題目連接:https://leetcode.com/problems...html
不會,參考這個博客的解釋:
http://www.cnblogs.com/grandy...code
計算三個點的法向量(叉乘),任意三個點必須同正或同負。這樣判斷三點組成的兩邊角度是否小於180。注意考慮90度的狀況,這時候叉乘爲0。htm
public class Solution { public boolean isConvex(List<List<Integer>> points) { int prev = 0; int n = points.size(); for(int i = 0; i < n; i++) { // check the angle constructed by (p0, p1) and (p0, p2) List<Integer> p0 = points.get(i); List<Integer> p1 = points.get((i + 1) % n); List<Integer> p2 = points.get((i + 2) % n); int dx1 = p0.get(0) - p1.get(0), dx2 = p0.get(0) - p2.get(0); int dy1 = p0.get(1) - p1.get(1), dy2 = p0.get(1) - p2.get(1); int cur = getProduct(dx1, dx2, dy1, dy2); // product different if((prev < 0 && cur > 0) || (prev > 0 && cur < 0)) return false; prev = (cur == 0 ? prev : cur); } return true; } private int getProduct(int dx1, int dx2, int dy1, int dy2) { return dx1 * dy2 - dx2 * dy1; } }