美文网首页
299. Bulls and Cows

299. Bulls and Cows

作者: April63 | 来源:发表于2018-06-15 11:13 被阅读0次

果然昨天焦虑了,嗯 ,其实最然方法不一定好,但是理一理思路还是清晰的,首先扫描一遍secret 与对应guess位置比较,完全相同肯定是a,这个时候要count+1同时需要记录下这个位置,如果不相同,把数值记录下来。第二遍扫描guess,如果是a的位置跳过,如果不是,这个数值如果存在再字典中,字典中数目-1,代码如下:

class Solution(object):
    def getHint(self, secret, guess):
        """
        :type secret: str
        :type guess: str
        :rtype: str
        """
        if len(secret) == 0:
            return "0A0B"
        zd = {}
        zda = []
        counta = 0
        countb = 0
        for i in range(len(secret)):
            if i < len(guess):
                if guess[i] == secret[i]:
                    counta += 1
                    zda.append(i)
                else:
                    if secret[i] not in zd:
                        zd[secret[i]] = {i}
                    else:
                        zd[secret[i]].add(i)
        for i in range(len(guess)):
            if i not in zda:
                if guess[i] in zd:
                    countb += 1
                    zd[guess[i]].pop()
                    if len(zd[guess[i]]) == 0:
                        del zd[guess[i]]
        return str(counta) + "A" + str(countb) + "B"

相关文章

  • 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)游戏:你写下一个数字让你的朋友...

  • 299. Bulls and Cows [Medium] 数组

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

  • Leetcode-Java(三十)

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

网友评论

      本文标题:299. Bulls and Cows

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