美文网首页
299. Bulls and Cows [Medium] 数组

299. Bulls and Cows [Medium] 数组

作者: 一个想当大佬的菜鸡 | 来源:发表于2019-07-18 10:05 被阅读0次

    299. Bulls and Cows

    299. Bulls and Cows

    刚开始没看懂题目,这个问题很简单的,就是要统计两个数组中,有哪些是位置对,值也对,有哪些是值对,位置不对,没什么难度

    class Solution(object):
        def getHint(self, secret, guess):
            """
            :type secret: str
            :type guess: str
            :rtype: str
            """
            A = B = 0
            count_1 = [0] * 10
            count_2 = [0] * 10
            for i in range(len(secret)):
                if secret[i] == guess[i]:
                    A += 1
                else:
                    s = int(secret[i])
                    g = int(guess[i])
                    count_1[s] += 1
                    count_2[g] += 1
            for i in range(10):
                B += min(count_1[i], count_2[i])
            return str(A) + "A" + str(B) + "B"
    
    class Solution(object):
        def getHint(self, secret, guess):
            """
            :type secret: str
            :type guess: str
            :rtype: str
            """
            A = B = 0
            count = [0] * 10
            for i in range(len(secret)):
                if secret[i] == guess[i]:
                    A += 1
                else:
                    s = int(secret[i])
                    g = int(guess[i])
                    if count[s] < 0: 
                        B += 1
                    if count[g] > 0:
                        B += 1
                    count[s] += 1
                    count[g] -= 1
            return str(A) + "A" + str(B) + "B"
    

    相关文章

      网友评论

          本文标题:299. Bulls and Cows [Medium] 数组

          本文链接:https://www.haomeiwen.com/subject/bbjklctx.html