Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a9eecb

Browse files
feat: add solutions to lc problem: No.1041 (doocs#628)
No.1041.Robot Bounded In Circle
1 parent 0faeb29 commit 9a9eecb

File tree

4 files changed

+82
-44
lines changed

4 files changed

+82
-44
lines changed

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,42 @@
6565
<!-- 这里可写当前语言的特殊实现逻辑 -->
6666

6767
```python
68-
68+
class Solution:
69+
def isRobotBounded(self, instructions: str) -> bool:
70+
direction = [0] * 4
71+
cur = 0
72+
for i in range(len(instructions)):
73+
if instructions[i] == 'L':
74+
cur = cur + 1 if cur < 3 else 0
75+
elif instructions[i] == 'R':
76+
cur = cur - 1 if cur > 0 else 3
77+
else:
78+
direction[cur] += 1
79+
return cur != 0 or (direction[0] == direction[2] and direction[1] == direction[3])
6980
```
7081

7182
### **Java**
7283

7384
<!-- 这里可写当前语言的特殊实现逻辑 -->
7485

7586
```java
76-
87+
class Solution {
88+
public boolean isRobotBounded(String instructions) {
89+
int[] direction = new int[4];
90+
int cur = 0;
91+
char[] chars = instructions.toCharArray();
92+
for (char c : chars) {
93+
if (c == 'L') {
94+
cur = cur < 3 ? cur + 1 : 0;
95+
} else if (c == 'R') {
96+
cur = cur > 0 ? cur - 1 : 3;
97+
} else {
98+
direction[cur] += 1;
99+
}
100+
}
101+
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]);
102+
}
103+
}
77104
```
78105

79106
### **...**

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,40 @@ When repeating these instructions, the robot remains in the circle of radius 2 c
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def isRobotBounded(self, instructions: str) -> bool:
60+
direction = [0] * 4
61+
cur = 0
62+
for i in range(len(instructions)):
63+
if instructions[i] == 'L':
64+
cur = cur + 1 if cur < 3 else 0
65+
elif instructions[i] == 'R':
66+
cur = cur - 1 if cur > 0 else 3
67+
else:
68+
direction[cur] += 1
69+
return cur != 0 or (direction[0] == direction[2] and direction[1] == direction[3])
5970
```
6071

6172
### **Java**
6273

6374
```java
64-
75+
class Solution {
76+
public boolean isRobotBounded(String instructions) {
77+
int[] direction = new int[4];
78+
int cur = 0;
79+
char[] chars = instructions.toCharArray();
80+
for (char c : chars) {
81+
if (c == 'L') {
82+
cur = cur < 3 ? cur + 1 : 0;
83+
} else if (c == 'R') {
84+
cur = cur > 0 ? cur - 1 : 3;
85+
} else {
86+
direction[cur] += 1;
87+
}
88+
}
89+
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]);
90+
}
91+
}
6592
```
6693

6794
### **...**
Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,17 @@
11
class Solution {
22
public boolean isRobotBounded(String instructions) {
3-
int col = 0;
4-
int row = 0;
5-
char[] orders = instructions.toCharArray();
6-
int order = 0;
7-
for (int i = 0; i < 4; i++) {
8-
for (char ch : orders) {
9-
if (ch == 'L') {
10-
order--;
11-
if (order == -3) {
12-
order = 1;
13-
}
14-
} else if (ch == 'R') {
15-
order++;
16-
if (order == 2) {
17-
order = -2;
18-
}
19-
} else {
20-
switch (order) {
21-
case 0:
22-
row++;
23-
break;
24-
case 1:
25-
col++;
26-
break;
27-
case -1:
28-
col--;
29-
break;
30-
case -2:
31-
row--;
32-
break;
33-
default:
34-
break;
35-
}
36-
}
37-
}
38-
if (col == 0 && row == 0) {
39-
return true;
3+
int[] direction = new int[4];
4+
int cur = 0;
5+
char[] chars = instructions.toCharArray();
6+
for (char c : chars) {
7+
if (c == 'L') {
8+
cur = cur < 3 ? cur + 1 : 0;
9+
} else if (c == 'R') {
10+
cur = cur > 0 ? cur - 1 : 3;
11+
} else {
12+
direction[cur] += 1;
4013
}
4114
}
42-
43-
return false;
15+
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]);
4416
}
45-
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def isRobotBounded(self, instructions: str) -> bool:
3+
direction = [0] * 4
4+
cur = 0
5+
for i in range(len(instructions)):
6+
if instructions[i] == 'L':
7+
cur = cur + 1 if cur < 3 else 0
8+
elif instructions[i] == 'R':
9+
cur = cur - 1 if cur > 0 else 3
10+
else:
11+
direction[cur] += 1
12+
return cur != 0 or (direction[0] == direction[2] and direction[1] == direction[3])

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /