Skip to content

Commit 49e2e8e

Browse files
committed
feat: add solutions to lc problem: No.1041
No.1041.Robot Bounded In Circle
1 parent ab8647b commit 49e2e8e

File tree

7 files changed

+147
-141
lines changed

7 files changed

+147
-141
lines changed

solution/1000-1099/1041.Robot Bounded In Circle/README.md

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,23 @@
8787

8888
<!-- 这里可写通用的实现逻辑 -->
8989

90-
定义 `cur` 表示初始方向(上),值为 0,`cur + 1``cur + 2``cur + 3` 分别表示 左、下、右。
90+
**方法一:模拟**
9191

92-
`direction[4]` 分别表示机器人在四个方向上行走的距离
92+
我们可以模拟机器人的行走过程,用一个变量 $k$ 表示机器人的方向,初始值为 $0$,表示机器人面向北方。变量 $k$ 的取值范围为 $[0, 3]$,分别表示机器人面向北、西、南、东。另外,我们用一个长度为 $4$ 的数组 $dist$ 记录机器人在四个方向上行走的距离,初始值为 $[0, 0, 0, 0]$
9393

94-
只要机器人最后的方向与初始方向 0 不一样,或者最后回到原点,返回 true。
94+
遍历指令字符串 `instructions`,如果当前指令为 `'L'`,那么机器人转向西方,即 $k = (k + 1) \bmod 4$;如果当前指令为 `'R'`,那么机器人转向东方,即 $k = (k + 3) \bmod 4$;否则,机器人在当前方向上行走一步,即 $dist[k]++$。
95+
96+
如果给定的指令字符串 `instructions` 执行一遍后,使得机器人最终回到原点,即 $dist[0] = dist[2]$ 且 $dist[1] = dist[3]$,那么机器人一定会进入循环。因为无论重复多少次指令,机器人都回到了原点,所以机器人一定会进入循环。
97+
98+
如果给定的指令字符串 `instructions` 执行一遍后,机器人没回到原点,但是机器人的方向与初始方向不同,即 $k \neq 0$,那么机器人也一定会进入循环。为什么?我们不妨假设在执行一遍指令过后,机器人位于 $(x, y)$,且方向为 $k$。
99+
100+
- 若 $k=1$,即机器人面向西方,那么机器人执行第二遍指令后,坐标变化量是 $(-y, x)$;继续执行第三遍执行后,坐标变化量是 $(-x, -y)$;继续执行第四遍指令后,坐标变化量是 $(y, -x)$。累加这些坐标变化量,我们可以发现,机器人最终会回到原点 $(0, 0)$;
101+
- 若 $k=2$,即机器人面向南方,那么执行第二遍指令后,坐标变化量是 $(-x, -y)$,累加这两次坐标变化量,我们可以发现,机器人最终会回到原点 $(0, 0)$;
102+
- 若 $k=3$,即机器人面向东方,那么执行第二遍指令后,坐标变化量是 $(y, -x)$;继续执行第三遍指令后,坐标变化量是 $(-x, -y)$;继续执行第四遍指令后,坐标变化量是 $(-y, x)$。累加这些坐标变化量,我们可以发现,机器人最终会回到原点 $(0, 0)$。
103+
104+
综上所述,如果给定的指令字符串 `instructions` 执行一遍后,机器人回到了原点,或者机器人的方向与初始方向不同,那么机器人一定会进入循环。
105+
106+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为指令字符串 `instructions` 的长度。
95107

96108
<!-- tabs:start -->
97109

@@ -102,17 +114,16 @@
102114
```python
103115
class Solution:
104116
def isRobotBounded(self, instructions: str) -> bool:
105-
cur, direction = 0, [0] * 4
106-
for ins in instructions:
107-
if ins == 'L':
108-
cur = (cur + 1) % 4
109-
elif ins == 'R':
110-
cur = (cur + 3) % 4
117+
k = 0
118+
dist = [0] * 4
119+
for c in instructions:
120+
if c == 'L':
121+
k = (k + 1) % 4
122+
elif c == 'R':
123+
k = (k + 3) % 4
111124
else:
112-
direction[cur] += 1
113-
return cur != 0 or (
114-
direction[0] == direction[2] and direction[1] == direction[3]
115-
)
125+
dist[k] += 1
126+
return (dist[0] == dist[2] and dist[1] == dist[3]) or k != 0
116127
```
117128

118129
### **Java**
@@ -122,18 +133,19 @@ class Solution:
122133
```java
123134
class Solution {
124135
public boolean isRobotBounded(String instructions) {
125-
int[] direction = new int[4];
126-
int cur = 0;
127-
for (char c : instructions.toCharArray()) {
136+
int k = 0;
137+
int[] dist = new int[4];
138+
for (int i = 0; i < instructions.length(); ++i) {
139+
char c = instructions.charAt(i);
128140
if (c == 'L') {
129-
cur = (cur + 1) % 4;
141+
k = (k + 1) % 4;
130142
} else if (c == 'R') {
131-
cur = (cur + 3) % 4;
143+
k = (k + 3) % 4;
132144
} else {
133-
++direction[cur];
145+
++dist[k];
134146
}
135147
}
136-
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]);
148+
return (dist[0] == dist[2] && dist[1] == dist[3]) || (k != 0);
137149
}
138150
}
139151
```
@@ -144,17 +156,18 @@ class Solution {
144156
class Solution {
145157
public:
146158
bool isRobotBounded(string instructions) {
147-
vector<int> direction(4);
148-
int cur = 0;
149-
for (char c : instructions) {
150-
if (c == 'L')
151-
cur = (cur + 1) % 4;
152-
else if (c == 'R')
153-
cur = (cur + 3) % 4;
154-
else
155-
++direction[cur];
159+
int dist[4]{};
160+
int k = 0;
161+
for (char& c : instructions) {
162+
if (c == 'L') {
163+
k = (k + 1) % 4;
164+
} else if (c == 'R') {
165+
k = (k + 3) % 4;
166+
} else {
167+
++dist[k];
168+
}
156169
}
157-
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]);
170+
return (dist[0] == dist[2] && dist[1] == dist[3]) || k;
158171
}
159172
};
160173
```
@@ -163,40 +176,37 @@ public:
163176
164177
```go
165178
func isRobotBounded(instructions string) bool {
166-
direction := make([]int, 4)
167-
cur := 0
168-
for _, ins := range instructions {
169-
if ins == 'L' {
170-
cur = (cur + 1) % 4
171-
} else if ins == 'R' {
172-
cur = (cur + 3) % 4
179+
dist := [4]int{}
180+
k := 0
181+
for _, c := range instructions {
182+
if c == 'L' {
183+
k = (k + 1) % 4
184+
} else if c == 'R' {
185+
k = (k + 3) % 4
173186
} else {
174-
direction[cur]++
187+
dist[k]++
175188
}
176189
}
177-
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3])
190+
return (dist[0] == dist[2] && dist[1] == dist[3]) || k != 0
178191
}
179192
```
180193

181194
### **TypeScript**
182195

183196
```ts
184197
function isRobotBounded(instructions: string): boolean {
185-
const direction = new Array(4).fill(0);
186-
let cur = 0;
187-
for (const c of instructions.split('')) {
198+
const dist: number[] = new Array(4).fill(0);
199+
let k = 0;
200+
for (const c of instructions) {
188201
if (c === 'L') {
189-
cur = (cur + 1) % 4;
202+
k = (k + 1) % 4;
190203
} else if (c === 'R') {
191-
cur = (cur + 3) % 4;
204+
k = (k + 3) % 4;
192205
} else {
193-
++direction[cur];
206+
++dist[k];
194207
}
195208
}
196-
return (
197-
cur !== 0 ||
198-
(direction[0] === direction[2] && direction[1] === direction[3])
199-
);
209+
return (dist[0] === dist[2] && dist[1] === dist[3]) || k !== 0;
200210
}
201211
```
202212

solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -89,36 +89,36 @@ Based on that, we return true.
8989
```python
9090
class Solution:
9191
def isRobotBounded(self, instructions: str) -> bool:
92-
cur, direction = 0, [0] * 4
93-
for ins in instructions:
94-
if ins == 'L':
95-
cur = (cur + 1) % 4
96-
elif ins == 'R':
97-
cur = (cur + 3) % 4
92+
k = 0
93+
dist = [0] * 4
94+
for c in instructions:
95+
if c == 'L':
96+
k = (k + 1) % 4
97+
elif c == 'R':
98+
k = (k + 3) % 4
9899
else:
99-
direction[cur] += 1
100-
return cur != 0 or (
101-
direction[0] == direction[2] and direction[1] == direction[3]
102-
)
100+
dist[k] += 1
101+
return (dist[0] == dist[2] and dist[1] == dist[3]) or k != 0
103102
```
104103

105104
### **Java**
106105

107106
```java
108107
class Solution {
109108
public boolean isRobotBounded(String instructions) {
110-
int[] direction = new int[4];
111-
int cur = 0;
112-
for (char c : instructions.toCharArray()) {
109+
int k = 0;
110+
int[] dist = new int[4];
111+
for (int i = 0; i < instructions.length(); ++i) {
112+
char c = instructions.charAt(i);
113113
if (c == 'L') {
114-
cur = (cur + 1) % 4;
114+
k = (k + 1) % 4;
115115
} else if (c == 'R') {
116-
cur = (cur + 3) % 4;
116+
k = (k + 3) % 4;
117117
} else {
118-
++direction[cur];
118+
++dist[k];
119119
}
120120
}
121-
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]);
121+
return (dist[0] == dist[2] && dist[1] == dist[3]) || (k != 0);
122122
}
123123
}
124124
```
@@ -129,17 +129,18 @@ class Solution {
129129
class Solution {
130130
public:
131131
bool isRobotBounded(string instructions) {
132-
vector<int> direction(4);
133-
int cur = 0;
134-
for (char c : instructions) {
135-
if (c == 'L')
136-
cur = (cur + 1) % 4;
137-
else if (c == 'R')
138-
cur = (cur + 3) % 4;
139-
else
140-
++direction[cur];
132+
int dist[4]{};
133+
int k = 0;
134+
for (char& c : instructions) {
135+
if (c == 'L') {
136+
k = (k + 1) % 4;
137+
} else if (c == 'R') {
138+
k = (k + 3) % 4;
139+
} else {
140+
++dist[k];
141+
}
141142
}
142-
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]);
143+
return (dist[0] == dist[2] && dist[1] == dist[3]) || k;
143144
}
144145
};
145146
```
@@ -148,40 +149,37 @@ public:
148149
149150
```go
150151
func isRobotBounded(instructions string) bool {
151-
direction := make([]int, 4)
152-
cur := 0
153-
for _, ins := range instructions {
154-
if ins == 'L' {
155-
cur = (cur + 1) % 4
156-
} else if ins == 'R' {
157-
cur = (cur + 3) % 4
152+
dist := [4]int{}
153+
k := 0
154+
for _, c := range instructions {
155+
if c == 'L' {
156+
k = (k + 1) % 4
157+
} else if c == 'R' {
158+
k = (k + 3) % 4
158159
} else {
159-
direction[cur]++
160+
dist[k]++
160161
}
161162
}
162-
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3])
163+
return (dist[0] == dist[2] && dist[1] == dist[3]) || k != 0
163164
}
164165
```
165166

166167
### **TypeScript**
167168

168169
```ts
169170
function isRobotBounded(instructions: string): boolean {
170-
const direction = new Array(4).fill(0);
171-
let cur = 0;
172-
for (const c of instructions.split('')) {
171+
const dist: number[] = new Array(4).fill(0);
172+
let k = 0;
173+
for (const c of instructions) {
173174
if (c === 'L') {
174-
cur = (cur + 1) % 4;
175+
k = (k + 1) % 4;
175176
} else if (c === 'R') {
176-
cur = (cur + 3) % 4;
177+
k = (k + 3) % 4;
177178
} else {
178-
++direction[cur];
179+
++dist[k];
179180
}
180181
}
181-
return (
182-
cur !== 0 ||
183-
(direction[0] === direction[2] && direction[1] === direction[3])
184-
);
182+
return (dist[0] === dist[2] && dist[1] === dist[3]) || k !== 0;
185183
}
186184
```
187185

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
class Solution {
22
public:
33
bool isRobotBounded(string instructions) {
4-
vector<int> direction(4);
5-
int cur = 0;
6-
for (char c : instructions) {
7-
if (c == 'L')
8-
cur = (cur + 1) % 4;
9-
else if (c == 'R')
10-
cur = (cur + 3) % 4;
11-
else
12-
++direction[cur];
4+
int dist[4]{};
5+
int k = 0;
6+
for (char& c : instructions) {
7+
if (c == 'L') {
8+
k = (k + 1) % 4;
9+
} else if (c == 'R') {
10+
k = (k + 3) % 4;
11+
} else {
12+
++dist[k];
13+
}
1314
}
14-
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]);
15+
return (dist[0] == dist[2] && dist[1] == dist[3]) || k;
1516
}
1617
};
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
func isRobotBounded(instructions string) bool {
2-
direction := make([]int, 4)
3-
cur := 0
4-
for _, ins := range instructions {
5-
if ins == 'L' {
6-
cur = (cur + 1) % 4
7-
} else if ins == 'R' {
8-
cur = (cur + 3) % 4
2+
dist := [4]int{}
3+
k := 0
4+
for _, c := range instructions {
5+
if c == 'L' {
6+
k = (k + 1) % 4
7+
} else if c == 'R' {
8+
k = (k + 3) % 4
99
} else {
10-
direction[cur]++
10+
dist[k]++
1111
}
1212
}
13-
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3])
13+
return (dist[0] == dist[2] && dist[1] == dist[3]) || k != 0
1414
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
class Solution {
22
public boolean isRobotBounded(String instructions) {
3-
int[] direction = new int[4];
4-
int cur = 0;
5-
for (char c : instructions.toCharArray()) {
3+
int k = 0;
4+
int[] dist = new int[4];
5+
for (int i = 0; i < instructions.length(); ++i) {
6+
char c = instructions.charAt(i);
67
if (c == 'L') {
7-
cur = (cur + 1) % 4;
8+
k = (k + 1) % 4;
89
} else if (c == 'R') {
9-
cur = (cur + 3) % 4;
10+
k = (k + 3) % 4;
1011
} else {
11-
++direction[cur];
12+
++dist[k];
1213
}
1314
}
14-
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]);
15+
return (dist[0] == dist[2] && dist[1] == dist[3]) || (k != 0);
1516
}
1617
}

0 commit comments

Comments
 (0)