Skip to content

Commit a30d513

Browse files
committed
Robot return to origin.
1 parent 9fab3f3 commit a30d513

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
There is a robot starting at the position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.
3+
4+
You are given a string moves that represents the move sequence of the robot where moves[i] represents its ith move. Valid moves are 'R' (right), 'L' (left), 'U' (up), and 'D' (down).
5+
6+
Return true if the robot returns to the origin after it finishes all of its moves, or false otherwise.
7+
8+
Note: The way that the robot is "facing" is irrelevant. 'R' will always make the robot move to the right once, 'L' will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.
9+
10+
 
11+
12+
Example 1:
13+
Input: moves = "UD"
14+
Output: true
15+
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.
16+
17+
Example 2:
18+
Input: moves = "LL"
19+
Output: false
20+
Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.
21+
 
22+
23+
Constraints:
24+
- 1 <= moves.length <= 2 * 10^4
25+
- moves only contains the characters 'U', 'D', 'L' and 'R'.
26+
*/
27+
class Solution {
28+
func judgeCircle(_ moves: String) -> Bool {
29+
var leftStack = [Character]()
30+
var rightStack = [Character]()
31+
var upStack = [Character]()
32+
var downStack = [Character]()
33+
for e in moves {
34+
switch e {
35+
case "L":
36+
leftStack.append(e)
37+
case "R":
38+
rightStack.append(e)
39+
case "U":
40+
upStack.append(e)
41+
case "D":
42+
downStack.append(e)
43+
default:
44+
break
45+
}
46+
}
47+
return leftStack.count == rightStack.count && upStack.count == downStack.count
48+
}
49+
}
50+
51+
let s = Solution()
52+
let r = s.judgeCircle("RRDD")
53+
print(r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/657.Robot Return to Origin.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
115. [Maximum Product of Three Numbers](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/628.Maximum%20Product%20of%20Three%20Numbers.playground/Contents.swift)
120120
116. [Maximum Average Subarray I](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/643.Maximum%20Average%20Subarray%20I.playground/Contents.swift)
121121
117. [Set Mismatch](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/645.Set%20Mismatch.playground/Contents.swift)
122+
118. [Robot Return to Origin](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/657.Robot%20Return%20to%20Origin.playground/Contents.swift)
122123

123124
#### Medium
124125

0 commit comments

Comments
 (0)