首页 > 试题广场 >

寻找Coder

[编程题]寻找Coder
  • 热度指数:26876 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

请设计一个高效算法,再给定的字符串数组中,找到包含"Coder"的字符串(不区分大小写),并将其作为一个新的数组返回。结果字符串的顺序按照"Coder"出现的次数递减排列,若两个串中"Coder"出现的次数相同,则保持他们在原数组中的位置关系。

给定一个字符串数组A和它的大小n,请返回结果数组。保证原数组大小小于等于300,其中每个串的长度小于等于200。同时保证一定存在包含coder的字符串。

测试样例:
["i am a coder","Coder Coder","Code"],3
返回:["Coder Coder","i am a coder"]
class Coder:
    def findCoder(self, A, n):
        result = []
        for i in A:
            if i.count('coder') != 0:
                result.append([i.count('coder'),i])   #保存出现次数,之后以出现次数排序
        result.sort(key=lambda x:x[0],reverse=True)
        return [i[1] for i in result]
编辑于 2018-10-20 11:09:23 回复(0)
python大法好
class Coder:
    def findCoder(self, A, n):
        return sorted(list(filter(lambda c:"coder" in c.lower(),A)),key=lambda c:c.lower().count("coder"),reverse=True)

发表于 2018-10-09 15:28:18 回复(0)
# -*- coding:utf-8 -*-
class Coder:
    def findCoder(self, A, n):
        A=sorted(A,key=lambda x:x.count('Coder')+x.count('coder'),reverse=True)#按出现次数排序
        A=filter(lambda x:x.count('Coder')+x.count('coder')>0,A)#过滤掉次数为0的字符串
        return A

发表于 2018-07-25 14:51:44 回复(0)

python四行:

def findCoder(self, A, n):
        a = list(filter(lambda c: "coder" in c.lower(), A))
        b = list(map(lambda c: c.lower().count("coder"), a))
        c = (sorted(zip(a, b), key=lambda c: c[1], reverse=True))
        return list(map(lambda x: x[0], c))
编辑于 2018-09-21 13:02:25 回复(4)
class Coder:
    def findCoder(self, A, n):
        # write code here
        bb = [b.lower() for b in A ]
        AA=sorted(bb,key=lambda x: x.count('coder'),reverse=True)
        
        for i in range(0,n):
            if AA[i].count('coder') == 0:
                break
        
        return AA[0:i] if i<n-1 else AA

发表于 2017-06-16 17:29:11 回复(0)
class Coder:
    def findCoder(self, A, n):
        # write code here
        repeattimes=[]
        last=[]
        for i in xrange(0,n):
                tmplist=str(A[i]).lower().split('coder')
                repeattimes.append(len(tmplist)-1)
        for j in xrange(0,n):
                if repeattimes[j]==0:
                        A.pop(j)
                        repeattimes.pop(j)
        while(1):
                if len(A)==0:
                        break
                last.append(A[repeattimes.index(max(repeattimes))])
                A.remove(A[repeattimes.index(max(repeattimes))])
                repeattimes.remove(max(repeattimes))
        return last
发表于 2017-04-24 16:58:20 回复(0)
【求助】本地可以运行成功,在牛客上就gg了
class Coder:
    def findCoder(self, A, n):
        # write code here
        if n <= 0 :
            return False
        dict1 = {}
        for i in range(n):
 cnt1 = len(A[i].split('coder'))
            cnt2 = len(A[i].split('Coder'))
            dict1[i] = cnt1+cnt2-2
        res = []
        l_sort = sorted(dict1.items(), key=lambda d: d[1],reverse = True)
        for key,value in l_sort:
            if value != 0:
                res.append(A[key])
        return res
发表于 2017-03-09 15:13:52 回复(0)

class Coder:
    def findCoder(self, A, n):
        # write code here
        t = []
        for i in A:
            if 'coder' in i.lower():
                t.append(i)
        t.sort(reverse = True,cmp = lambda x,y:x.lower().count('coder')-y.lower().count('coder'))
        return t
发表于 2016-09-16 00:00:09 回复(0)
一直报越界错误,没看出哪里有问题啊!!!!
# -*- coding:utf-8 -*-
import traceback  
class Coder:
    def findCoder(self, A, n):
        if(n<=300):
            counts = []
            ans = []
            #存储所有的计数值
            for i in A:
                if(len(i)<=200):
            count = i.count('coder')+i.count('Coder')
                counts.append(count)
                else:
                    return 0
            #每次取出最多coder出现最多次数的子串,确定其位置后放入ans,保证了从大到小,组内有序的要求。
            while(True):
                if(max(counts)!=-1)
               maxs = max(counts)
                    num = counts.index(maxs)
                    #要注意的是,这里如果直接删除会使得原列表顺序发生改变,不应该删除而应该改为-1
                    counts[num] = -1
                    ans.append(A[num])
                else:
                    return ans
        else:
            return 0  
发表于 2016-06-21 15:02:40 回复(0)