Easy to understand JAVA Solution


  • 0
    J
    public class Solution {
        public boolean judgeCircle(String moves) {
            
            int horizontal      = 0;
            int vertical        = 0;
            
            for(int i = 0; i < moves.length(); i++){
                
                switch(moves.charAt(i)){
                    case 'U':
                        vertical++;
                        break;
                    case 'D':
                        vertical--;
                        break;
                    case 'R':
                        horizontal++;
                        break;
                    case 'L':
                        horizontal--;
                        break;
                }
                
            }
            
            return (horizontal == 0 && vertical == 0);
            
        }
        
    }
    

Log in to reply
 

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