Skip to content

Commit 521f44a

Browse files
committed
add leetcode solution
1 parent a3de2c0 commit 521f44a

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.mccreefei.technologystack.leetcode;
2+
3+
/**
4+
* @author MccreeFei
5+
* @create 2020-09-18 下午1:46
6+
* @refer <href>https://leetcode.com/problems/robot-bounded-in-circle/</href>
7+
* @idea 在一次指令全部走完后 如果机器人在起点或者机器人的前进方向改变 则走的是一个循环
8+
*/
9+
public class RobotBoundedInCircle {
10+
public boolean isRobotBounded(String instructions) {
11+
int d = 0, x = 0, y = 0;
12+
int[][] move = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
13+
for (int i = 0; i < instructions.length(); i++) {
14+
char c = instructions.charAt(i);
15+
if (c == 'R') {
16+
d = (d + 1) % 4;
17+
} else if (c == 'L') {
18+
d = (d + 3) % 4;
19+
} else {
20+
x += move[d][0];
21+
y += move[d][1];
22+
}
23+
}
24+
return (x == 0 && y == 0) || d != 0;
25+
}
26+
}

0 commit comments

Comments
 (0)