8-liner O(N) with clarification and suggestions for OJ


  • 0

    The solution should be straightforward, but I see in other posts that confusion is raised because of ambiguity in the problem. So it seems that the problem has the following assumptions:

    1. The robot is judged only by the final position;
    2. The directional moves LRUD are absolute (regardless of robot's facing direction), so maybe it is better to describe as true directions (W)EST, (E)AST, (N)ORTH and (S)OUTH.

    I didn't really participate in the contest, but I think we shouldn't get too emotional because of problem ambiguity since one of the coder's attributes should be rational, right? :-)
    @administrators

        bool judgeCircle(string moves) {
            int LR = 0, UD = 0;
            for (char c : moves)
                switch (c) {
                    case 'L': ++LR; break;
                    case 'R': --LR; break;
                    case 'U': ++UD; break;
                    case 'D': --UD; break;
                }
            return !LR && !UD;
        }
    

Log in to reply
 

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