leetcode - 500 - 键盘行

注意初始化

class Solution:
    def findWords(self, words):
        """
        :type words: List[str]
        :rtype: List[str]
        """
        L1 = ["q","w","e","r","t","y","u","i","o","p"]
        L2 = ["a","s","d","f","g","h","j","k","l"]
        L3 = ["z","x","c","v","b","n","m"]
        a=0
        b=0
        c=0
        out = []
        for word in words:
            wo = word.lower()
            for i in range(len(wo)):
                if wo[i] in L1:
                    a+=1
                    continue
                elif wo[i] in L2:
                    b+=1
                    continue
                else:
                    c+=1
                    continue
            if a==len(wo) or b==len(wo) or c==len(wo):
                out.append(word)
                a,b,c = 0,0,0     # 注意初始化
            else:
                a,b,c = 0,0,0     ####
                
        return out

猜你喜欢

转载自blog.csdn.net/hustwayne/article/details/83593008