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 tothe original place.
The move sequence is represented byastring. And each move is represent byacharacter. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be trueorfalse representing whether the robot makes a circle.
Example 1:
Input: "UD"
Output: true
Example 2:
Input: "LL"
Output: false
class Solution {
public:
booljudgeCircle(string moves) {
int cnt_ud = 0;
int cnt_lr = 0;
for(int i=0;i<moves.size();i++)
{
if(moves[i]=='U')
cnt_ud ++;
elseif(moves[i]=='D')
cnt_ud --;
elseif(moves[i]=='L')
cnt_lr ++;
else
cnt_lr --;
}
if (cnt_lr==0 && cnt_ud ==0)
returntrue;
elsereturnfalse;
}
};
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