为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode1041. 困于环中的机器人 | Robot Bounded In Circle

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

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

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

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

On an infinite plane, a robot initially stands at (0, 0) and faces north.  The robot can receive one of three instructions:

  • "G": go straight 1 unit;
  • "L": turn 90 degrees to the left;
  • "R": turn 90 degress to the right.

The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

Example 1:

Input: "GGLLGG"
Output: true
Explanation: 
The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).
When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.

Example 2:

Input: "GG"
Output: false
Explanation: 
The robot moves north indefinetely.

Example 3:

Input: "GL"
Output: true
Explanation: 
The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ...

Note:

  1. 1 <= instructions.length <= 100
  2. instructions[i] is in {'G', 'L', 'R'}

在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方。机器人可以接受下列三条指令之一:

  • "G":直走 1 个单位
  • "L":左转 90 度
  • "R":右转 90 度

机器人按顺序执行指令 instructions,并一直重复它们。

只有在平面中存在环使得机器人永远无法离开时,返回 true。否则,返回 false

示例 1:

输入:"GGLLGG"
输出:true
解释:
机器人从 (0,0) 移动到 (0,2),转 180 度,然后回到 (0,0)。
重复这些指令,机器人将保持在以原点为中心,2 为半径的环中进行移动。

示例 2:

输入:"GG"
输出:false
解释:
机器人无限向北移动。

示例 3:

输入:"GL"
输出:true
解释:
机器人按 (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ... 进行移动。

提示:

  1. 1 <= instructions.length <= 100
  2. instructions[i] 在 {'G', 'L', 'R'} 中

0ms
 1 class Solution {
 2     enum dir {
 3         case px, py, nx, ny
 4         
 5         func turnL() -> dir
 6         {
 7             switch self {
 8             case .px: return .py
 9             case .py: return .nx
10             case .nx: return .ny
11             case .ny: return .px
12             }
13         }
14         
15         func trunR() -> dir
16         {
17             switch self {
18             case .px: return .ny
19             case .ny: return .nx
20             case .nx: return .py
21             case .py: return .px
22             }
23         }
24         
25         func move(point: inout [Int])
26         {
27             switch self {
28             case .px: point[0] += 1; return
29             case .py: point[1] += 1; return
30             case .nx: point[0] -= 1; return
31             case .ny: point[1] -= 1; return
32             }
33         }
34     }
35     
36     func isRobotBounded(_ instructions: String) -> Bool
37     {
38         if self.help(instructions) { return true }
39         else
40         {
41             var newInput = instructions
42             for _ in 1...3
43             {
44                 newInput.append(instructions)
45             }
46             return self.help(newInput)
47         }
48     }
49     
50     private func help(_ instructions: String) -> Bool
51     {
52         var d: dir = .px
53         // index0: x, index1: y
54         var p = [0,0]
55         
56         for c in instructions
57         {
58             if c == "G" { d.move(point: &p) }
59             else if c == "L" { d = d.turnL() }
60             else { d = d.trunR() }
61         }
62         
63         return p[0] == 0 && p[1] == 0
64     }
65 }

Runtime: 4 ms

Memory Usage: 20.5 MB
 1 class Solution {
 2     func isRobotBounded(_ instructions: String) -> Bool {
 3         let dx:[Int] = [0,-1,0,1]
 4         let dy:[Int] = [1,0,-1,0]
 5         let arr:[Character] = Array(instructions)
 6         var dir:Int = 0
 7         var x:Int = 0
 8         var y:Int = 0
 9         for i in 0..<arr.count
10         {
11             switch arr[i]
12             {
13                 case "L":
14                 dir += 1
15                 case "R":
16                 dir += 3
17                 default:
18                 x += dx[dir]
19                 y += dy[dir]
20             }
21             dir %= 4
22         }
23         return !((x != 0 || y != 0) && dir == 0)
24     }
25 }

Runtime: 8 ms

Memory Usage: 20.6 MB
 1 class Solution {
 2     func isRobotBounded(_ instructions: String) -> Bool {
 3         let dx:[Int] = [0,-1,0,1]
 4         let dy:[Int] = [1,0,-1,0]
 5         let arr:[Character] = Array(instructions)
 6         var dir:Int = 0
 7         var x:Int = 0
 8         var y:Int = 0
 9         for i in 0..<arr.count
10         {
11             switch arr[i]
12             {
13                 case "L":
14                 dir += 1
15                 case "R":
16                 dir += 3
17                 default:
18                 x += dx[dir]
19                 y += dy[dir]
20             }
21             dir %= 4
22         }
23         if dir != 0 {return true}
24         if x == 0 && y == 0 {return true}
25         return false
26     }
27 }

4ms

 1 class Solution {
 2     let directions = [(0, 1), (-1, 0), (0, -1), (1, 0)]
 3     func isRobotBounded(_ instructions: String) -> Bool {
 4 
 5         var index = 0
 6         var location = (0, 0)
 7         // for _ in 1...4 {
 8             location = getOnePass(instructions, location, &index)
 9         // }
10         return location.0 == 0 && location.1 == 0 || index > 0
11     }
12 
13     fileprivate func getOnePass(_ instructions: String, _ start: (Int, Int), _ index:inout Int) -> (Int, Int) {
14         
15         var curr = start
16         for item in instructions {
17             if item == "G" {
18                 curr = (curr.0 + directions[index].0, curr.1 + directions[index].1)
19             } else if item == "L" {
20                 index = (index + 3) % 4
21             } else if item == "R" {
22                 index = (index + 1) % 4
23             }
24         }
25         return curr
26     }
27 }

 

posted @ 2019-05-12 12:11  为敢技术  阅读(653)  评论(5编辑  收藏  举报