文章目录
  1. 1. 文档更新说明
  2. 2. 前言
  3. 3. 771. Jewels and Stones
    1. 3.1. Example1
    2. 3.2. Example2
    3. 3.3. Note
    4. 3.4. Solution

文档更新说明

  • 最后更新 2018年03月08日
  • 首次更新 2018年03月08日

前言

  使用Swift语言完成LetCode题目,简单难度

771. Jewels and Stones

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

Example1

Input: "UD"
Output: true  

Example2

Input: "LL"
Output: false

Note

太简单了没有提示……

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import Foundation

class Solution {
func judgeCircle(_ moves: String) -> Bool {
var x = 0, y = 0
for c in moves {
switch c {
case "U":
y += 1
case "D":
y -= 1
case "L":
x -= 1
case "R":
x += 1
default:break
}
}
return x == 0 && y == 0
}
}

print(Solution().judgeCircle("LL"))

文章目录
  1. 1. 文档更新说明
  2. 2. 前言
  3. 3. 771. Jewels and Stones
    1. 3.1. Example1
    2. 3.2. Example2
    3. 3.3. Note
    4. 3.4. Solution