美文网首页
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] 数组

    299. Bulls and Cows 刚开始没看懂题目,这个问题很简单的,就是要统计两个数组中,有哪些是位置对,...

  • 2019-02-09

    LeetCode 299. Bulls and Cows Description You are playing ...

  • 299. Bulls and Cows

    Description You are playing the following Bulls and Cows ...

  • 299. Bulls and Cows

    果然昨天焦虑了,嗯 ,其实最然方法不一定好,但是理一理思路还是清晰的,首先扫描一遍secret 与对应guess位...

  • 299. Bulls and Cows

    一遍过。

  • 299. Bulls and Cows

    问题 You are playing the following Bulls and Cows game with...

  • 299. Bulls and Cows

    You are playing the following Bulls and Cows game with yo...

  • LeetCode*299. Bulls and Cows

    LeetCode题目链接 注意:凡是以英文出现的,都是题目提供的,包括答案代码里的前几行。 题目: You are...

  • 299. Bulls and Cows(easy)

    introduction 你正在和你的朋友玩 猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友...

  • Leetcode-Java(三十)

    299. Bulls and Cows 一开始我用的是HashSet保存两个字符串中出现过的数字但是没有匹配上的,...

网友评论

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

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