c++ simple single pass code


  • 0
    G
    class Solution {
    public:
        bool isSelfCrossing(vector<int>& x) {
            if(x.size() < 4) return false;
            bool bInc = x[2] > x[0];
            for(int i = 3; i < x.size(); i++){
                if(bInc){
                    //increasing spiral
                    if(x[i] <= x[i - 2]){
                        bInc = false;
                        //handle border
                        if(x[i] >= x[i - 2] - (i < 4 ? 0 : x[i-4]) )
                            x[i-1] -= x[i-3];
                    }
                }
                else{
                    //decreasing spiral
                    if(x[i] >= x[i-2]) return true;
                }
            }
            return false;
        }
    };
    

Log in to reply
 

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