500. Keyboard Row

python只在数字类型之间有隐式转换,不能隐式的从数字转到string。
str1.find(str2),找到就返回位置,否则返回-1

class Solution(object):
    def findWords(self, words):
        """
        :type words: List[str]
        :rtype: List[str]
        """
        a='qwertyuiopQWERTYUIIOP'
        b='asdfghjklASDFGHJKL'
        c='zxcvbnmZXCVBNM'
        test=list()
        outcome=list()
        n=0
        ifDel=False
        for word in words:
            j=0
            for alf in word:
                if alf in a:
                    test.append('a')
                    if j>0 and test[j]!=test[j-1]:
                        ifDel=True
                        break
                    j+=1
                elif alf in b:
                    test.append('b')
                    if j>0 and test[j]!=test[j-1]:
                        ifDel=True
                        break
                    j+=1
                elif alf in c:
                    test.append('c')
                    if j>0 and test[j]!=test[j-1]:
                        ifDel=True
                        break
                    j+=1
            if not ifDel:
                outcome.append(word)
                n+=1
            else:
                ifDel=False
            test=[]
        return outcome
         
       妈的血的教训!!!不要再for循环的时候改变正在循环的那个list!!!我日,改了之后,你在要用这个list的时候很容易出错,比如你删掉了这个list的第一个元素,然后你的索引全部变了,但是循环还是继续在往后走!!可以看看这些答案。。
https://www.zhihu.com/question/49098374

另外,有一个复制list的办法: a=b[:](据说最好不要用这个)而建议使用new_list = list(old_list)
(class list([iterable])

Return a list whose items are the same and in the same order as iterable’s items. iterable may be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a list, a copy is made and returned, similar to iterable[:]. For instance, list('abc') returns ['a', 'b', 'c'] and list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, returns a new empty list, [].
list is a mutable sequence type, as documented in Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange. For other containers see the built in dict, set, and tuple classes, and the collections module.)

关于复制list,这个也讲得很详细:https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list

这是一个思路和我一样的……但是人家把代码写的很清晰:
class Solution(object): def findWords(self, words): """ :type words: List[str] :rtype: List[str] """ row1 = 'q w e r t y u i o p' row2 = 'a s d f g h j k l' row3 = 'z x c v b n m' row1 = (row1 + ' ' + row1.upper()).split(' ') row2 = (row2 + ' ' + row2.upper()).split(' ') row3 = (row3 + ' ' + row3.upper()).split(' ') result = [] for word in words: ch0 = word[0] if ch0 in row1: row = row1 elif ch0 in row2: row = row2 else: row = row3 flag = True for ch in word: if ch not in row: flag = False break if flag: result.append(word) return result

思路是每次比较单词的第一个字母,然后后面的字母和第一个字母比较(我用的后一个字母和前一个字母比较!!失之毫厘差之千里啊我日),如果和第一个字母不一样就筛选出来。虽然这个人速度和我的差不多,但是代码可读性比我的强多了。

然后这个是最快的答案!!!也很好!!用到了set!!!非常好用!这个才是正确解法!!!:
class Solution(object): def findWords(self, words): """ :type words: List[str] :rtype: List[str] """ _set = set row1 = _set('qwertyuiopQWERTYUIOP') row2 = _set('asdfghjklASDFGHJKL') row3 = _set('zxcvbnmZXCVBNM') row_words = [] for w in words: w_set = _set(w) if w_set <= row1 or w_set <= row2 or w_set <= row3: row_words.append(w) return row_words
和第二快的:
class Solution(object): def findWords(self, words): """ :type words: List[str] :rtype: List[str] """ ans=list() for i in words: temp=str(i).lower() if len(set("qwertyuiop"+temp))==len("qwertyuiop") or len(set("asdfghjkl"+temp))==len("asdfghjkl") or len(set("zxcvbnm"+temp))==len("zxcvbnm"): ans.append(i) return ans

还有人用正则表达式来写的……mark一下到时候可以研究一下

评论

此博客中的热门博文

225 Implement Stack using Queues

232. Implement Queue using Stacks

20. Valid Parentheses