·
·
文章目录
  1. 题目介绍
  2. 复杂度
  3. 解题思路

Robot Return to Origin

题目介绍

LeetCode 657. Robot Return to Origin

复杂度

时间复杂度: O(n), 空间复杂度: O(1)

解题思路

因为设定每次移动幅度相同,所以用两个变量分别存储水平和垂直方向的值,只要 string 里面 U 和 D 的数量一致,L 和 R 的数量一致即可回到原点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
func judgeCircle(_ moves: String) -> Bool {
var v = 0
var h = 0
moves.map {
switch $0 {
case "U" : v -= 1
case "D" : v += 1
case "L" : h -= 1
case "R" : h += 1
default: break
}
}
return v == 0 && h == 0
}
}
**版权声明**

Ivan’s Blog by Ivan Ye is licensed under a Creative Commons BY-NC-ND 4.0 International License.
叶帆创作并维护的叶帆的博客博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证

本文首发于Ivan’s Blog | 叶帆的博客博客( http://yeziahehe.com ),版权所有,侵权必究。

本文链接:http://yeziahehe.com/2020/02/27/RobotReturntoOrigin/

支持一下
扫一扫,支持yeziahehe
  • 微信扫一扫
  • 支付宝扫一扫