@@ -0,0 +1,2177 @@
+public class SelfCrossing %7B%0A%0A%09// For this question, to keep the line not crossed, it can be in following conditions:%0A%09// Keep spiraling outside.%0A%09// Keep spiraling inside.%0A%09// Not crossing during transition from outside spiral into inside spiral.%0A    public boolean isSelfCrossingDefineWhenIts_Not_Crossing(int%5B%5D x) %7B%0A        if(x == null %7C%7C x.length %3C= 3) return false;%0A        return !isSpinIn(x, 0, x.length - 1) && !isSpinOut(x, 0, x.length - 1);%0A    %7D%0A    // spin-in, if cross, return false.%0A    private boolean isSpinIn(int%5B%5D x, int from, int end) %7B%0A        for(int i = from, j = from + 2; j %3C= end; i++, j++)%0A            if(i != from && x%5Bi%5D %3C= x%5Bj%5D) %0A                return false;%0A        return true;%0A    %7D%0A    // spin-out / spin-out to spin in.%0A    private boolean isSpinOut(int%5B%5D x, int from, int end) %7B%0A        for(int i = from, j = from + 2; j %3C end; i++, j++)%0A            if(x%5Bi%5D %3E= x%5Bj%5D) %0A                // not crossing when spin-out change to spin-in and the violated part is a valid spin-in%0A                return (i - 2 %3E= 0 && x%5Bj%5D %3C (x%5Bi%5D - x%5Bi-2%5D) %7C%7C i - 1 %3E= 0 && x%5Bj+1%5D %3C (x%5Bi + 1%5D - x%5Bi-1%5D)) && isSpinIn(x, i, end);%0A        return true;%0A    %7D%0A%0A%0A%0A    // Categorize the self-crossing scenarios, there are 3 of them: %0A%09// 1. Fourth line crosses first line and works for fifth line crosses second line and so on...%0A%09// 2. Fifth line meets first line and works for the lines after%0A%09// 3. Sixth line crosses first line and works for the lines after%0A%09public boolean isSelfCrossingDefineWhenItsCrossing(int%5B%5D x) %7B%0A        int l = x.length;%0A        if(l %3C= 3) return false;%0A        %0A        for(int i = 3; i %3C l; i++)%7B%0A            if(x%5Bi%5D %3E= x%5Bi-2%5D && x%5Bi-1%5D %3C= x%5Bi-3%5D) return true;  //Fourth line crosses first line and onward%0A            if(i %3E=4)%0A            %7B%0A                if(x%5Bi-1%5D == x%5Bi-3%5D && x%5Bi%5D + x%5Bi-4%5D %3E= x%5Bi-2%5D) return true; // Fifth line meets first line and onward%0A            %7D%0A            if(i %3E=5)%0A            %7B%0A                if(x%5Bi-2%5D - x%5Bi-4%5D %3E= 0 && x%5Bi%5D %3E= x%5Bi-2%5D - x%5Bi-4%5D && x%5Bi-1%5D %3E= x%5Bi-3%5D - x%5Bi-5%5D && x%5Bi-1%5D %3C= x%5Bi-3%5D) return true;  // Sixth line crosses first line and onward%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%7D

add self crossing.