Skip to content

Commit 1d6c82e

Browse files
committed
Solve problem 657. Judge Route Circle
1 parent a9e8c5c commit 1d6c82e

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

judge_route_circle/main.go

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,71 @@
11
package judge_route_circle
22

3-
func judgeCircle(moves string) bool {
4-
var result bool
3+
import (
4+
"fmt"
5+
"strings"
6+
)
57

8+
type Robot struct {
9+
position Position
10+
}
11+
12+
type Position struct {
13+
x int
14+
y int
15+
}
16+
17+
func (r *Robot) moveRight() {
18+
r.position.y++
19+
20+
}
21+
22+
func (r *Robot) moveLeft() {
23+
r.position.y--
24+
25+
}
26+
27+
func (r *Robot) moveUp() {
28+
r.position.x++
29+
30+
}
31+
32+
func (r *Robot) moveDown() {
33+
r.position.x--
34+
35+
}
36+
37+
func (r *Robot) Move(moves string) {
38+
39+
for _, step := range moves {
40+
switch strings.ToUpper(string(step)) {
41+
case "R":
42+
r.moveRight()
43+
case "L":
44+
r.moveLeft()
45+
case "U":
46+
r.moveUp()
47+
case "D":
48+
r.moveDown()
49+
default:
50+
fmt.Printf("Wrong movement!")
51+
}
52+
}
53+
54+
}
55+
56+
func judgeCircle(moves string) (result bool) {
57+
58+
robot := Robot{
59+
position: Position{
60+
x: 0,
61+
y: 0,
62+
},
63+
}
64+
65+
robot.Move(moves)
66+
if robot.position.x == 0 && robot.position.y == 0 {
67+
result = true
68+
}
669
return result
770

871
}

0 commit comments

Comments
 (0)