python leetcode练习(500.键盘行)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python leetcode练习(500.键盘行)相关的知识,希望对你有一定的参考价值。

class Solution:                    
    def findWords(self, words):
        """
        :type words: List[str]
        :rtype: List[str]
        """    
        # judge which row the input char belongs to
        def judgeRow(c):
            row1 =['q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P']
            row2 =['a','s','d','f','g','h','j','k','l','A','S','D','F','G','H','J','K','L']
            row3 =['z','x','c','v','b','n','m','Z','X','C','V','B','N','M']
            if(row1.count(c)):
                return 1
            elif(row2.count(c)):
                return 2
            else:
                return 3
            
        listReturn = []   # to store the final retured list, outside the loop 
        for word in words:                                     
            wordRecover = ''
            rowNum = judgeRow(word[0]) # judge the row from the first char
            #print (rowNum)
            for c in word:                     
                rowCurrent = judgeRow(c)                
                print (rowCurrent)
                if(rowCurrent != rowNum):
                    wordRecover = ''
                    break;
                wordRecover += c
                print (wordRecover)
            if(wordRecover!=''):
                listReturn.append(wordRecover)
        return listReturn

以上是关于python leetcode练习(500.键盘行)的主要内容,如果未能解决你的问题,请参考以下文章

leetcode500. 键盘行

力扣(LeetCode)500. 键盘行

Leetcode——500. 键盘行(Java)

LeetCode 500. Keyboard Row (键盘行)

leetcode 500. 键盘行(Keyboard Row)

Leetcode#500. Keyboard Row(键盘行)