Leetcode - Self Crossing

My code:

public class Solution {
    public boolean isSelfCrossing(int[] x) {
        if (x == null || x.length <= 3) {
            return false;
        }
        
        for (int i = 3; i < x.length; i++) {
            if (x[i] >= x[i - 2] && x[i - 3] >= x[i - 1]) {
                return true;
            }
            if (i >= 4) {
                if (x[i - 1] == x[i - 3] && x[i] + x[i - 4] >= x[i - 2]) {
                    return true;
                }
            }
            if (i >= 5) {
                if (x[i - 2] >= x[i - 4] && x[i] + x[i - 4] >= x[i - 2] && x[i - 1] + x[i - 5] >= x[i - 3] && x[i - 3] >= x[i - 1]) {
                    return true;
                }
            }
        }
        
        return false;
    }
}

reference:
https://discuss.leetcode.com/topic/38014/java-oms-with-explanation

根据分析,一共可能出现这三种自旋。
然后根据三种情况,分别写出判断,就行了。

Anyway, Good luck, Richardo! -- 10/09/2016

你可能感兴趣的:(Leetcode - Self Crossing)