Judge Route Circle


  • 0

    Click here to see the full article post


  • 0
    A

    def circleRobot(moves):
    counter = 0
    counterL = 0
    counterR = 0
    counterD = 0
    for i in range(len(moves)):
    if moves[i] == 'U':
    counter+=1
    if moves[i] == 'L':
    counterL +=1
    if moves[i] == 'R':
    counterR += 1
    if moves[i] == 'D':
    counterD += 1
    if counter == counterD and counterL == counterR:
    return True
    else:
    return False


  • 0
    F

    class Solution:
    def judgeCircle(self, moves):
    if moves.count('U') == moves.count('D') and moves.count('R') == moves.count('L'):
    return True
    else:
    return False


  • 0
    B
    class Solution:
        def judgeCircle(self, moves):
            return (moves.count('U') == moves.count('D')) and (moves.count('L') == moves.count('R'))
    

  • 0
    K

    class Solution:
    def init(self):
    self.position = [0, 0]

    def judgeCircle(self, moves):
        """
        :type moves: str
        :rtype: bool
        """
        while moves:
            move = moves[0]
            if move == "U":
                self.position[0] += 1
            elif move == "D":
                self.position[0] -= 1
            elif move == "R":
                self.position[1] += 1
            else:
                self.position[1] -= 1
            moves = moves[1:]
    
        return self.position == [0, 0]

  • 0
    S

    In the code, h means horizontal, v means vertical.

    java

    class Solution {
        public boolean judgeCircle(String moves) {
            int h = 0;
            int v = 0;
            for(char c : moves.toCharArray()){
                switch (c){
                    case 'U':
                        v += 1;break;
                    case 'D':
                        v -= 1;break;
                    case 'L':
                        h -= 1;break;
                    case 'R':
                        h += 1;break;
                }
            }
            return (h == 0 && v == 0);
        }
    }
    

    python

    class Solution:
        def judgeCircle(self, moves):
            """
            :type moves: str
            :rtype: bool
            """
            h,v = 0,0
            for c in moves:
                if c == "U":
                    v += 1
                elif c == "D":
                    v -= 1
                elif c == "R":
                    h += 1
                else:
                    h -= 1
            return v == 0 and h == 0        
    

    javascript

    var judgeCircle = function(moves) {
        var [v,h] = [0, 0];
        for(c of moves){
            switch(c){
                case 'U': v += 1;break;
                case 'D': v -= 1;break;
                case 'R': h += 1;break;
                case 'L': h -= 1;break;
            }        
        }
        return (v==0 && h==0);
    };
    

    ruby

    def judge_circle(moves)
        h,v = 0,0
        moves.each_char {|c|
        case c
            when 'U'
                v += 1
            when 'D'
                v -= 1
            when 'R'
                h += 1
            when 'L'
                h -= 1
        end
        }
        v == 0 && h == 0
    end
    

    golang

    func judgeCircle(moves string) bool {
        var v,h int
        for _, c := range moves {
            switch c {
                case 'U':
                  v += 1
                case 'D':
                  v -= 1
                case 'R':
                  h += 1
                case 'L':
                  h -= 1
            }    
        }
        return v == 0 && h == 0
    }
    

  • 0
    M

    Python
    class Solution:
    def judgeCircle(self, moves):
    """
    :type moves: str
    :rtype: bool
    """
    moves = moves.upper()
    return moves.count('L')==moves.count('R') and moves.count('U')==moves.count('D')


  • 0
    S

    why my code is not give the right answer????????

    class Solution {
    public:
    bool judgeCircle(string moves) {

        int u,d,r,l,rr;
        r=d=r=l=0;
        
        int ll=moves.size();
        
        for(int i=0;i<ll;i++)    {
            if(moves[i]=='U')   u++;
            else if(moves[i]=='D')  d++;
            else if(moves[i]=='R')  r++;
            else if(moves[i]=='L')  l++;
        }
        
        if(u==d && l==r)    return true;
        else    return false;
    }
    

    };


  • 0
    N

    If the string length is an odd number, it can be used as an early exit condition.


  • 0
    K

    @shunno021 you have a syntax error when you intialize the variables


  • 0
    S

    object Solution {
    def judgeCircle(moves: String): Boolean = {
    var pos : Int= 0
    moves.foreach{x=>
    { x match {
    case 'U' => pos=pos+1
    case 'D' => pos=pos-1
    case 'L' => pos=pos-1
    case 'R' => pos=pos+1
    }
    }
    }
    if(pos==0) true else false
    }
    }


  • 0
    S

    def solution(moves: String) : Boolean = {
    var pos = 0
    moves.foreach{
    case 'L' | 'D' => pos+=1
    case 'R' | 'U' => pos-=1
    }
    if(pos==0) true else false
    }


  • 0
    H

    @fengxuchen Can you not just return the boolean value of the statement moves.count('U') == moves.count('D') and moves.count('R') == moves.count('L')? This If/Else is unnecessary.


  • 0
    H

    @shunno021 There are a few different things that we can do to clean this up a bit. First, at the end of your method you have return statements inside of an If/Else. This is not necessary. The boolean expression that you are using in your If statement would work just fine as your return statement. I.E. "return u==d && l==r;". Second, you have a ton of variables that you are managing that you don't really need. The ints that you have to manage each direction can be combined into either one or at most two, one for u/d and one for l/r. Also what is rr? I don't think you are even using it. and ll is another unnecessary variable assignment. You can just use moves.size() in your for-loop. Lastly, I think that a Switch statement would be more appropriate in this case in place of the four If/Else statements nested in your for-loop. Since we have a finite number move types and they are easily represented by chars, a switch statement is a cleaner look (albeit not functionally different) and will help in debugging.


  • 0
    P

    java Solution

    class Solution {
        public boolean judgeCircle(String moves) {
            
            char[] myArray = moves.toCharArray();
            
            int hCounter = 0;
            int vCounter = 0;
            
            for (int i = 0; i<myArray.length;i++){
                if (myArray[i]=='U'){
                    hCounter++;
                }
                if (myArray[i]=='D'){
                    hCounter--;
                }
                if (myArray[i]=='R'){
                    vCounter++;
                }
                if (myArray[i]=='L'){
                    vCounter--;
                }
               
            }
             if (hCounter == 0 && vCounter ==0){
                    return true;
                }
            return false;
        }
    }

  • 0
    S

    Ruby solution:
    def judge_circle(moves)
    position = [0,0]
    moves = moves.split("")
    moves.each do |move|
    if move == "U"
    position[1] = position[1] + 1
    elsif
    move == "D"
    position[1] = position[1] - 1
    elsif
    move == "L"
    position[0] = position[0] - 1
    elsif
    move == "R"
    position[0] = position[0] + 1
    end
    end
    if position[0] == 0 && position[1] == 0
    return true
    else return false
    end
    end


  • 0
    W

    Could someone explain the complexity analysis for this solution?

    Python solution:
    class Solution:
    def judgeCircle(self, moves):
    """
    :type moves: str
    :rtype: bool
    """
    total_direction = 0

        moves_dict = {"R": 1, "L": -1, "U": 10, "D": -10}
        for direction in moves:
            total_direction += moves_dict[direction]
        
        if total_direction == 0:
            return True
        else:
            return False

  • 0
    W

    Could someone explain the complexity analysis for this solution?

    Python solution:

    class Solution:
    def judgeCircle(self, moves):
    total_direction = 0
    
        moves_dict = {"R": 1, "L": -1, "U": 10, "D": -10}
        for direction in moves:
            total_direction += moves_dict[direction]
        
        if total_direction == 0:
            return True
        else:
            return False
    

  • 0
    R

    @withoutwax 1. for moves_dict: O(1) space complexity 2. O(n) run time, as you are going through each move


  • 0
    A

    public static boolean robot(String robotMove) {
    int robotMoveLen = robotMove.length();
    int ULength = robotMoveLen - robotMove.replace("U","").length();
    int LLength = robotMoveLen - robotMove.replace("L","").length();
    int RLength = robotMoveLen - robotMove.replace("R","").length();
    int DLength = robotMoveLen - robotMove.replace("D","").length();
    if(ULength == DLength && LLength == RLength) {
    return true;
    }
    return false;
    }


Log in to reply
 

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