A slow but easy to understand solution


  • 0
    Y

    follow the path and save the seen integral points. if seen again then it's self crossing.

    class Solution(object):
        def isSelfCrossing(self, x):
            """
            :type x: List[int]
            :rtype: bool
            """
            seen={(0,0)}
            a,b=0,0
            ds=[[0,1],[-1,0],[0,-1],[1,0]]
            for i in xrange(len(x)):
                da,db=ds[i%4]
                for _ in xrange(x[i]):
                    a,b=a+da,b+db
                    if (a,b) in seen:
                        return True
                    seen.add((a,b))
            return False
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.