机器人用C ++回归原点
为了解决这个问题,我们将遵循以下步骤-
l:=移动数组的大小
如果l与0相同,则-
返回真
lft:=0,向上:=0
对于初始化i:=0,当i<l时,更新(将i增加1),执行-
(减少1)
(增加1)
(将英尺降低1)
(将英尺增加1)
如果moves[i]与“L”相同,则-
如果moves[i]与'R'相同,则-
如果moves[i]与“U”相同,则-
如果moves[i]与'D'相同,则-
如果lft等于0,up等于0,则-
返回真
返回假
例
让我们看下面的实现以更好地理解-
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool judgeCircle(string moves) {
int l = moves.length();
if (l == 0) {
return true;
}
int lft = 0, up = 0;
for (int i = 0; i < l; i++) {
if (moves[i] == 'L') {
lft++;
}
if (moves[i] == 'R') {
lft--;
}
if (moves[i] == 'U') {
up++;
}
if (moves[i] == 'D') {
up--;
}
}
if (lft == 0 && up == 0) {
return true;
}
return false;
}
};
main(){
Solution ob;
cout << (ob.judgeCircle("RRULLD"));
}输入值
"RRULLD"
输出结果
1