文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
Robot Return to Origin2. Solution
- Version 1
class Solution {
public:
bool judgeCircle(string moves) {
int vertical = 0;
int horizontal = 0;
for(char ch : moves) {
if(ch == 'L') {
horizontal--;
}
else if(ch == 'R') {
horizontal++;
}
else if(ch == 'U') {
vertical++;
}
else {
vertical--;
}
}
return vertical == 0 && horizontal == 0;
}
};
- Version 2
class Solution {
public:
bool judgeCircle(string moves) {
int vertical = 0;
int horizontal = 0;
for(char ch : moves) {
switch(ch) {
case 'L':
horizontal--;
break;
case 'R':
horizontal++;
break;
case 'U':
vertical++;
break;
case 'D':
vertical--;
break;
}
}
return vertical == 0 && horizontal == 0;
}
};
网友评论