机器人用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
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短