Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L(Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

Example 1:

Input: "UD"
Output: true

 

Example 2:

Input: "LL"
Output: false

public class Solution {
    public boolean judgeCircle(String moves) {
        if (moves == null)
            return true;
        int x = 0, y = 0;
        for (int i=0; i<moves.length(); i++) {
            char ch = moves.charAt(i);
            if (ch == 'U')
                y ++;
            else if (ch == 'D')
                y --;
            else if (ch == 'L')
                x --;
            else if (ch == 'R')
                x ++;
        }
        return x==0 && y==0;
    }
}

 

相关文章:

  • 2021-07-16
  • 2021-07-31
  • 2021-08-10
  • 2022-12-23
  • 2022-03-06
  • 2021-05-09
  • 2021-11-11
猜你喜欢
  • 2021-07-26
  • 2022-12-23
  • 2021-11-24
  • 2021-11-05
  • 2022-12-23
  • 2021-09-16
相关资源
相似解决方案