Java solution for Judge Route Circle


  • 0
    C

    Please find my simple solution for Judge Route Circle

    class Solution {
        public boolean judgeCircle(String moves) {
            int[] result = new int[]{0,0};
            
            for(int i=0; i<moves.length(); i++){
                char c = moves.charAt(i);
                
                if(c == 'U'){
                    result[0] = result[0] + 1;
                }
                
                if(c == 'D'){
                    result[0] = result[0] - 1;
                }
                
                 if(c == 'L'){
                    result[1] = result[1] + 1;
                }
                
                 if(c == 'R'){
                    result[1] = result[1] - 1;
                }
            }
            
            if(result[0]== 0 && result[1]==0){
                return true;
            }
            return false;
        }
    }
    

    Logic is like this,
    We are considering that (0,0) i s our starting point.

    • U --> 1, 0
    • D --> -1, 0
    • L --> 0, 1
    • R --> 0, -1
      In array doing addition and subtraction on index 0 & 1

Log in to reply
 

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