299. Bulls and Cows

xiaoxiao2021-02-28  95

class Solution(object):     def getHint(self, secret, guess):         """         :type secret: str         :type guess: str         :rtype: str         """         bulls=0         crows=0         m={}         for i in range(len(secret)):             if secret[i]==guess[i]:                 bulls+=1             else:                 if secret[i] not in m:                     m[secret[i]]=1                 else:                     m[secret[i]]+=1         for j in range(len(secret)):             if secret[j]!=guess[j] and guess[j] in m:                 if m[guess[j]]>0:                     crows+=1                 m[guess[j]]-=1

        return '%sA%sB'%(bulls,crows)

思路参考:

http://www.cnblogs.com/grandyang/p/4929139.html

转载请注明原文地址: https://www.6miu.com/read-73133.html

最新回复(0)