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

思路:

思路比较简单,就是用x记录x方向位置,y记录y方向位置。最后如果还在原点的话说明有circle。

bool judgeCircle(string moves)
{
        int x=0,y=0;
    for(auto c:moves)
    {
      if(c == 'U')y++;
      if(c == 'D')y--;
      if(c == 'L')x--;
      if(c == 'R')x++;
    }
    if(x==0&&y==0)return true;
    return false;
    }

 

相关文章:

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