方法一:

思路:使用两个变量记录横纵坐标,然后模拟就行了

  1. func judgeCircle(moves string) bool {
  2. //记录横纵坐标
  3. x, y := 0, 0
  4. for _, ch := range moves {
  5. if ch == 'L' {
  6. x--
  7. } else if ch == 'R' {
  8. x++
  9. } else if ch == 'U' {
  10. y--
  11. } else {
  12. y++
  13. }
  14. }
  15. return x == 0 && y == 0
  16. }