Skip to content

Commit cb2daef

Browse files
solves robot return to origin
1 parent 2578ae8 commit cb2daef

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
| 643 | [Maximum Average SubArray I](https://leetcode.com/problems/maximum-average-subarray-i) | [![Java](assets/java.png)](src/MaximumAverageSubArrayI.java) [![Python](assets/python.png)](python/maximum_average_subarray_I.py) |
170170
| 645 | [Set Mismatch](https://leetcode.com/problems/set-mismatch) | [![Java](assets/java.png)](src/SetMismatch.java) [![Python](assets/python.png)](python/set_mismatch.py) |
171171
| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst) | [![Java](assets/java.png)](src/TwoSumIVInputIsABST.java) [![Python](assets/python.png)](python/two_sum_iv.py) |
172-
| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin) | |
172+
| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin) | [![Java](assets/java.png)](src/RobotReturnToOrigin.java) [![Python](assets/python.png)](python/robot_return_to_origin.py) |
173173
| 661 | [Image Smoother](https://leetcode.com/problems/image-smoother) | |
174174
| 665 | [Non Deceasing Array](https://leetcode.com/problems/non-decreasing-array) | |
175175
| 669 | [Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree) | |

Diff for: python/robot_return_to_origin.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def judgeCircle(self, moves: str) -> bool:
3+
vertical, horizontal = 0, 0
4+
for move in moves:
5+
if move == 'U': vertical += 1
6+
elif move == 'D': vertical -= 1
7+
elif move == 'R': horizontal += 1
8+
elif move == 'L': horizontal -= 1
9+
return vertical == 0 and horizontal == 0

Diff for: src/RobotReturnToOrigin.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class RobotReturnToOrigin {
2+
public boolean judgeCircle(String moves) {
3+
int vertical = 0, horizontal = 0;
4+
for (int index = 0 ; index < moves.length() ; index++) {
5+
switch (moves.charAt(index)) {
6+
case 'U' -> vertical++;
7+
case 'R' -> horizontal++;
8+
case 'D' -> vertical--;
9+
case 'L' -> horizontal--;
10+
}
11+
}
12+
return vertical == 0 && horizontal == 0;
13+
}
14+
}

0 commit comments

Comments
 (0)