Easy to understand Java solution


  • 0
    T
    public boolean judgeCircle(String moves) {
        int row = 0;
        int col = 0;
        
        for( int i=0; i<moves.length(); i++ ){
            char c = moves.charAt(i);
            if( c == 'U' ){
                col++;
            }
            else if( c == 'D' ){
                col--;
            }
            else if( c=='L'){
                row--;
            }
            else{
                row++;
            }            
        }
        if( row==0 && col==0 ) return true;
        else return false;
    }

Log in to reply
 

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