zoukankan      html  css  js  c++  java
  • 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
    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    Ubuntu下RabbitMq 安装与运行
    web_api所需包
    Ubuntu16.04下安装python3.6.4详细步骤
    JavaScript
    css
    html
    MySQL
    day4 函数
    day3 字典,集合,文件
    day2
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11631448.html
Copyright © 2011-2022 走看看