leetcode——1002.查找常用字符

class Solution:
    def commonChars(self, A):
        A.sort(reverse=True)
        #print(A)
        x=len(A[0])
        l=[]
        for i in range(len(A)):
            x=min(x,len(A[i]))
        #print(x)
        for j in A[0]:
            for i in range(1,len(A)):
                if j not in A[i]:
                    break
            else:
                l.append(j)
                #print(j)
                for i in range(1,len(A)):
                    #print(list(A[i]))
                    c=list(A[i])
                    c.remove(str(j))
                    c=''.join(c)
                    A[i]=c
                    #print(A[i])        
        return l
执行用时 :80 ms, 在所有 Python3 提交中击败了43.61%的用户 内存消耗 :13.9 MB, 在所有 Python3 提交中击败了5.24%的用户   别人48ms的例子:
class Solution:
    def commonChars(self, A: List[str]) -> List[str]:
        res=[]
        if not A:
            return res
        key=set(A[0])
        for k in key:
            minnum=min(a.count(k) for a in A)
            res+=minnum*k
        return res

我怎么就想不到这样的办法?

minnum=min(a.count(k) for a in A)
 这个用法好棒!!!                                                                              ——2019.10.7

 

上一篇:【leetcode】1002. Find Common Characters


下一篇:1002 A+B for Polynomials