Skip to content

feat: add solutions to lc problem: No.1041 #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions solution/1000-1099/1041.Robot Bounded In Circle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,42 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def isRobotBounded(self, instructions: str) -> bool:
direction = [0] * 4
cur = 0
for i in range(len(instructions)):
if instructions[i] == 'L':
cur = cur + 1 if cur < 3 else 0
elif instructions[i] == 'R':
cur = cur - 1 if cur > 0 else 3
else:
direction[cur] += 1
return cur != 0 or (direction[0] == direction[2] and direction[1] == direction[3])
```

### **Java**

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

```java

class Solution {
public boolean isRobotBounded(String instructions) {
int[] direction = new int[4];
int cur = 0;
char[] chars = instructions.toCharArray();
for (char c : chars) {
if (c == 'L') {
cur = cur < 3 ? cur + 1 : 0;
} else if (c == 'R') {
cur = cur > 0 ? cur - 1 : 3;
} else {
direction[cur] += 1;
}
}
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]);
}
}
```

### **...**
Expand Down
31 changes: 29 additions & 2 deletions solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,40 @@ When repeating these instructions, the robot remains in the circle of radius 2 c
### **Python3**

```python

class Solution:
def isRobotBounded(self, instructions: str) -> bool:
direction = [0] * 4
cur = 0
for i in range(len(instructions)):
if instructions[i] == 'L':
cur = cur + 1 if cur < 3 else 0
elif instructions[i] == 'R':
cur = cur - 1 if cur > 0 else 3
else:
direction[cur] += 1
return cur != 0 or (direction[0] == direction[2] and direction[1] == direction[3])
```

### **Java**

```java

class Solution {
public boolean isRobotBounded(String instructions) {
int[] direction = new int[4];
int cur = 0;
char[] chars = instructions.toCharArray();
for (char c : chars) {
if (c == 'L') {
cur = cur < 3 ? cur + 1 : 0;
} else if (c == 'R') {
cur = cur > 0 ? cur - 1 : 3;
} else {
direction[cur] += 1;
}
}
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]);
}
}
```

### **...**
Expand Down
52 changes: 12 additions & 40 deletions solution/1000-1099/1041.Robot Bounded In Circle/Solution.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,17 @@
class Solution {
public boolean isRobotBounded(String instructions) {
int col = 0;
int row = 0;
char[] orders = instructions.toCharArray();
int order = 0;
for (int i = 0; i < 4; i++) {
for (char ch : orders) {
if (ch == 'L') {
order--;
if (order == -3) {
order = 1;
}
} else if (ch == 'R') {
order++;
if (order == 2) {
order = -2;
}
} else {
switch (order) {
case 0:
row++;
break;
case 1:
col++;
break;
case -1:
col--;
break;
case -2:
row--;
break;
default:
break;
}
}
}
if (col == 0 && row == 0) {
return true;
int[] direction = new int[4];
int cur = 0;
char[] chars = instructions.toCharArray();
for (char c : chars) {
if (c == 'L') {
cur = cur < 3 ? cur + 1 : 0;
} else if (c == 'R') {
cur = cur > 0 ? cur - 1 : 3;
} else {
direction[cur] += 1;
}
}

return false;
return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]);
}
}
}
12 changes: 12 additions & 0 deletions solution/1000-1099/1041.Robot Bounded In Circle/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def isRobotBounded(self, instructions: str) -> bool:
direction = [0] * 4
cur = 0
for i in range(len(instructions)):
if instructions[i] == 'L':
cur = cur + 1 if cur < 3 else 0
elif instructions[i] == 'R':
cur = cur - 1 if cur > 0 else 3
else:
direction[cur] += 1
return cur != 0 or (direction[0] == direction[2] and direction[1] == direction[3])