LeetCode 657. Judge Route Circle
阿新 • • 發佈:2018-03-25
方式 刷題 這一 pan right 標簽 pla rdquo tar
(Up) and
題目標簽:String
題目讓我們來判定,一個機器人從出發點開始以 “上”, “下”, “左”, “右” 的方式來移動,最後是否回到了原點。
這一題還是挺容易的,只要把出發點設為x = 0, y = 0 ,然後把每一次的移動 加上1 或者減去1 就可以了。
具體請看Code。
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
D
(down). The output should be true or false representing whether the robot makes a circle.
Example 1:
Input: "UD" Output: true
Example 2:
Input: "LL" Output: false
題目標簽:String
距離上一次刷題已經是一個月之前了- -,因為終於找到了工作。上班一個月的感受是,練好英語很重要,比起寫代碼水平,首先你得把開會說的那些都聽懂是吧。所以大家要把對英語的重視程度提高到和刷題一樣!!!
Java Solution:
Runtime beats 72.84%
完成日期:03/24/2018
關鍵詞:坐標
關鍵點:出發點為 x = 0, y = 0
1 class Solution2 { 3 public boolean judgeCircle(String moves) 4 { 5 int x = 0; 6 int y = 0; 7 8 for(char c: moves.toCharArray()) 9 { 10 switch(c) 11 { 12 case ‘R‘: 13 x++; 14 break; 15 case ‘L‘: 16 x--; 17 break; 18 case ‘U‘: 19 y++; 20 break; 21 case ‘D‘: 22 y--; 23 break; 24 default: 25 System.out.println("Invalid move"); 26 } 27 } 28 29 30 return (x == 0 && y == 0); 31 } 32 }
參考資料:n/a
LeetCode 題目列表 - LeetCode Questions List
題目來源:https://leetcode.com/
LeetCode 657. Judge Route Circle