★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ngtguwtz-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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.git
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.github
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:this
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:spa
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) 處結束。code
移動順序由字符串表示。字符 move[i] 表示其第 i 次移動。機器人的有效動做有 R
(右),L
(左),U
(上)和 D
(下)。若是機器人在完成全部動做後返回原點,則返回 true。不然,返回 false。htm
注意:機器人「面朝」的方向可有可無。 「R」 將始終使機器人向右移動一次,「L」 將始終向左移動等。此外,假設每次移動機器人的移動幅度相同。blog
示例 1:ci
輸入: "UD" 輸出: true 解釋:機器人向上移動一次,而後向下移動一次。全部動做都具備相同的幅度,所以它最終回到它開始的原點。所以,咱們返回 true。
示例 2:
輸入: "LL" 輸出: false 解釋:機器人向左移動兩次。它最終位於原點的左側,距原點有兩次 「移動」 的距離。咱們返回 false,由於它在移動結束時沒有返回原點。
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 }