• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode657.机器人能否返回原点|RobotReturntoOrigin

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10485839.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

There is a robot starting at 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.

The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.

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.

Example 1:

Input: "UD"
Output: true 
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.

Example 2:

Input: "LL"
Output: false
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.

在二维平面上,有一个机器人从原点 (0, 0) 开始。给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束。

移动顺序由字符串表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R(右),L(左),U(上)和 D(下)。如果机器人在完成所有动作后返回原点,则返回 true。否则,返回 false。

注意:机器人“面朝”的方向无关紧要。 “R” 将始终使机器人向右移动一次,“L” 将始终向左移动等。此外,假设每次移动机器人的移动幅度相同。

示例 1:

输入: "UD"
输出: true
解释:机器人向上移动一次,然后向下移动一次。所有动作都具有相同的幅度,因此它最终回到它开始的原点。因此,我们返回 true。

示例 2:

输入: "LL"
输出: false
解释:机器人向左移动两次。它最终位于原点的左侧,距原点有两次 “移动” 的距离。我们返回 false,因为它在移动结束时没有返回原点。

Runtime: 32 ms
Memory Usage: 19.5 MB
 1 class Solution {
 2     func judgeCircle(_ moves: String) -> Bool {
 3         var x : Int = 0 , y : Int = 0
 4         for move in moves.unicodeScalars {
 5             switch move {
 6             case "L": x -= 1
 7             case "R": x += 1
 8             case "U": y -= 1
 9             case "D": y += 1
10             default: return false
11             }
12         }
13         return x == 0 && y == 0
14     }
15 }

40ms

 1 class Solution {
 2     func judgeCircle(_ moves: String) -> Bool {
 3         let arr = moves.unicodeScalars
 4         var v = 0, h = 0
 5         for char in arr {
 6             switch char {
 7             case "L":
 8                 v = v + 1
 9             case "R":
10                 v = v - 1
11             case "U":
12                 h = h + 1
13             case "D":
14                 h = h - 1
15             default:
16                 continue
17             }
18         }
19         
20         if v == 0 && h == 0 {
21             return true
22         } else {
23             return false
24         }
25     }
26 }

40ms

 1 class Solution {
 2     func judgeCircle(_ moves: String) -> Bool {
 3         var x = 0
 4         var y = 0
 5         var hArray: [Int] = Array<Int>(repeating: 0, count: 86)
 6         var vArray: [Int] = Array<Int>(repeating: 0, count: 86)
 7         hArray[Int("L".unicodeScalars.first!.value)] = 1
 8         hArray[Int("R".unicodeScalars.first!.value)] = -1
 9 
10         vArray[Int("U".unicodeScalars.first!.value)] = 1
11         vArray[Int("D".unicodeScalars.first!.value)] = -1
12         for i in moves.unicodeScalars {
13             let move = Int(i.value)
14             x += hArray[move]
15             y += vArray[move]
16         }
17         return x == 0 && y == 0
18     }
19 }

52ms

 1 class Solution {
 2     func judgeCircle(_ moves: String) -> Bool {
 3                var x = 0
 4         var y = 0
 5         let arr = Array(moves)
 6 
 7         for i in 0..<moves.count {
 8             let move = arr[i]
 9             switch move {
10             case "U":
11                 y += 1
12             case "D":
13                 y -= 1
14             case "L":
15                 x += 1
16             case "R":
17                 x -= 1
18             default: fatalError()
19             }
20         }
21         return x == 0 && y == 0
22     }
23 }

92ms

 1 class Solution {
 2     func judgeCircle(_ moves: String) -> Bool {
 3              var x = 0
 4         var y = 0
 5         let hDic: [Character: Int] = ["L": 1, "R": -1]
 6         let vDic: [Character: Int] = ["U": 1, "D": -1]
 7         let arr = Array(moves)
 8         let count = moves.count
 9         for i in 0..<count {
10             let move = arr[i]
11             x += hDic[move] ?? 0
12             y += vDic[move] ?? 0
13         }
14         return x == 0 && y == 0
15     }
16 }

124ms

 1 class Solution {
 2   func judgeCircle(_ moves: String) -> Bool {
 3     var directions: [Character:Int] = [
 4       "U" : 0,
 5       "D" : 0,
 6       "L" : 0,
 7       "R" : 0
 8     ]
 9     for move in moves {
10       directions.updateValue(directions[move]! + 1, forKey: move)
11     }
12     return directions["U"] == directions["D"] && directions["L"] == directions["R"]
13   }
14 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode525.连续数组|ContiguousArray发布时间:2022-07-13
下一篇:
Swift3 创建字典发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap