657. Judge Route Circle

題目地址:https://leetcode.com/problems/judge-route-circle/description/
大意:The valid robot moves are R (Right), L (Left), U (Up) and D (down). 給一個(gè)字符串,讓這個(gè)機(jī)器人按照字符串的字母走,看看能不能回到原點(diǎn)

其實(shí)很簡(jiǎn)單,看看R的個(gè)數(shù)和L的個(gè)數(shù)還有U的個(gè)數(shù)和D的個(gè)數(shù)這兩個(gè)都要相同就能回來了。

class Solution:
    def judgeCircle(self, moves):
        """
        :type moves: str
        :rtype: bool
        """
        sp = list(moves)
        if (sp.count('R') == sp.count('L') and sp.count('U') == sp.count('D')):
            return True
        else:
            return False

a = Solution()
print (a.judgeCircle('LL'))

改進(jìn):

def judgeCircle2(self, moves):
        """
        :type moves: str
        :rtype: bool
        """
        return moves.count('L') == moves.count('R') and moves.count('U') and moves.count('D')

知識(shí)點(diǎn):

string字符串是可以直接用count()方法的。返回值也不用判斷TrueFalse是可以直接返回判斷表達(dá)式就可以了。



所有題目解題方法和答案代碼地址:https://github.com/fredfeng0326/LeetCode
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容