摘要:題目描述解決方案解題思路設(shè)置初始坐標(biāo)為根據(jù)上下左右指示調(diào)整坐標(biāo)判斷最后坐標(biāo)的位置是否為起始位置。加強(qiáng)版循環(huán)使用比判斷快方法計(jì)算向左和向右的次數(shù)是否相同,計(jì)算向上和向下的次數(shù)相同。若都相同,則回到原地。
題目描述
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.
Example 1:
Input: "UD" Output: true
Example 2:
Input: "LL" Output: false解決方案 - C++
class Solution { public: bool judgeCircle(string moves) { int x = 0, y = 0; for(int i = 0; i < moves.length(); i++){ if(moves.substr(i, 1) == "R"){ x--; } if(moves.substr(i, 1) == "L"){ x++; } if(moves.substr(i, 1) == "U"){ y++; } if(moves.substr(i, 1) == "D"){ y--; } } if(x == 0 && y == 0){ return true; }else{ return false; } } };
解題思路
設(shè)置初始坐標(biāo)為(0,0),根據(jù)上U下D左L右R指示調(diào)整坐標(biāo),判斷最后坐標(biāo)的位置是否為起始位置。
Submission Details
62 / 62 test cases passed. Status: Accepted Runtime: 39 ms[C++] [Java] Clean Code
Tips:
1.加強(qiáng)版for循環(huán)
2.使用switch比if判斷快
3.Java toCharArray()方法
C++
class Solution { public: bool judgeCircle(string moves) { int v = 0; int h = 0; for (char ch : moves) { switch (ch) { case "U" : v++; break; case "D" : v--; break; case "R" : h++; break; case "L" : h--; break; } } return v == 0 && h == 0; } };
Submission Details
62 / 62 test cases passed. Status: Accepted Runtime: 19 ms
Java
class Solution{ public boolean judgeCircle(String moves) { int v = 0, h = 0; for (char move : moves.toCharArray()) { switch (move) { case "U": v++; break; case "D": v--; break; case "R": h++; break; case "L": h--; break; } } return v == 0 && h == 0; } }
Submission Details
62 / 62 test cases passed. Status: Accepted Runtime: 11 msPython one liner
class Solution(object): def judgeCircle(self, moves): """ :type moves: str :rtype: bool """ return moves.count("L") == moves.count("R") and moves.count("U") == moves.count("D")
Submission Details
62 / 62 test cases passed. Status: Accepted Runtime: 42 ms
計(jì)算向左和向右的次數(shù)是否相同,計(jì)算向上和向下的次數(shù)相同。若都相同,則回到原地。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/67723.html
摘要:題目鏈接題目分析輸入一串指令操作機(jī)器人,判斷執(zhí)行完指令后,能否回到原點(diǎn)。思路判斷向上移動的次數(shù)是否等于向下移動的次數(shù),且向左次數(shù)是否等于向右次數(shù)。但是,如果在指令中沒有出現(xiàn)所有種方向的話,在判斷時會獲取不到數(shù)值。 657. Robot Return to Origin 題目鏈接 657. Robot Return to Origin 題目分析 輸入一串指令操作機(jī)器人,判斷執(zhí)行完指令后,...
摘要:若函數(shù)不能執(zhí)行有效的轉(zhuǎn)換,返回。如果數(shù)值超過可表示的范圍,則返回或。示例輸入輸出解釋轉(zhuǎn)換截止于數(shù)字,因?yàn)樗南乱粋€字符不為數(shù)字。 這是我參與11月更文挑戰(zhàn)的第12天。一、寫在前面LeetCode 第一題兩數(shù)之和傳輸門:聽說你還在寫雙層for循環(huán)解兩數(shù)之和?LeetCode 第二題兩數(shù)之和傳輸門:兩個排序數(shù)組的中...
摘要:解題思路模擬題就按照題目意思來做題目分三種情況那我們也分三種情況通過設(shè)置用來標(biāo)志第一位是大寫還是小寫小寫的話直接通過后面的都不能大寫來判斷大寫的話用記錄后面的大寫的個數(shù)為或者為字符串長度才符合要求代碼如下代碼 ...
摘要:找規(guī)律復(fù)雜度時間空間思路仔細(xì)觀察格雷碼當(dāng)時當(dāng)時當(dāng)時可以發(fā)現(xiàn),的格雷碼,就是的格雷碼,再加上它們的逆序前面多一個。 Grey Code The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n re...
LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...
閱讀 923·2023-04-26 01:34
閱讀 3356·2023-04-25 20:58
閱讀 3263·2021-11-08 13:22
閱讀 2108·2019-08-30 14:17
閱讀 2522·2019-08-29 15:27
閱讀 2673·2019-08-29 12:45
閱讀 2996·2019-08-29 12:26
閱讀 2811·2019-08-28 17:51