Code & Func
2017-12-19

第83天。

今天的题目是:Judge Route Circle:

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

比较无聊的一道题目,我们只需要维护一组下标来记录所在的位置即可,然后判断移动完后是否回到了最开始的位置即可:

1
bool judgeCircle(string moves) {
2
int x = 0, y = 0;
3
for(auto c:moves) {
4
switch(c){
5
case 'U': y++; break;
6
case 'D': y--; break;
7
case 'L': x--; break;
8
case 'R': x++; break;
9
}
10
}
11
return x == 0 && y == 0;
12
}
上一条动态