My simple Java Solution using one count variable


  • 0
    R
    public boolean judgeCircle(String moves) {
            int count = 0;
            for(int i = 0; i  < moves.length(); i++){
                char currChar = moves.charAt(i);
                switch(currChar){
                    case 'U':
                        count += 1;
                        break;
                    case 'D':
                        count -= 1;
                        break;
                    case 'L':
                        count -= 1;
                        break;
                    case 'R':
                        count += 1;
                        break;
                }
            }
            return count == 0;
        }

  • 0
    A

    Won't work for DR or UL. You have to use different int for vertical and horizontal moves. Eg. 1 and 2.


Log in to reply
 

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