Solution without branches


  • 0
    M

    This is for fun. I'm not an optimization person, but I think this might help yield a vectorized solution:

        bool judgeCircleA(string moves) {
            int L = 0, R = 0;
            int U = 0, D = 0;
    
            for (auto ch : moves) {
                L += (ch == 'L');
                R -= (ch == 'R');
                U += (ch == 'U');
                D -= (ch == 'D');
            }
    
            return (L + R == 0) && (U + D) == 0;
        }
    

Log in to reply
 

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