Sharing my C++ solution


  • 1
    T
    class Solution {
    public:
        bool isSelfCrossing(vector<int>& x) {
            int i, n = x.size();
            for(i=0; i<n; i++)
            {
                if(i>=3 && x[i]>=x[i-2] && x[i-1]<=x[i-3])
                    return true;
                    
                if(i>=4 && x[i-1]==x[i-3] && x[i]+x[i-4]>=x[i-2])
                    return true;
                    
                if(i>=5 && x[i]+x[i-4]>=x[i-2] && x[i-1]+x[i-5]>=x[i-3] && x[i-2]>=x[i-4] && x[i-3]>=x[i-1])
                    return true;
            }
            
            return false;
        }
    };

Log in to reply
 

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