299. Bulls and Cows [easy] (Python)

来源:互联网 发布:java垃圾回收方法 编辑:程序博客网 时间:2024/05/24 04:04

题目链接

https://leetcode.com/problems/bulls-and-cows/

题目原文

You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called “bulls”) and how many digits match the secret number but locate in the wrong position (called “cows”). Your friend will use successive guesses and hints to eventually derive the secret number.

For example:
Secret number: “1807”
Friend’s guess: “7810”

Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.)
Write a function to return a hint according to the secret number and friend’s guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return "1A3B".

Please note that both secret number and friend’s guess may contain duplicate digits, for example:
Secret number: “1123”
Friend’s guess: “0111”

In this case, the 1st 1 in friend’s guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return "1A1B".
You may assume that the secret number and your friend’s guess only contain digits, and their lengths are always equal.

题目翻译

你正在跟你的朋友玩猜数字游戏(Bulls and Cows):你写一串数字让你的朋友猜。你的朋友每猜一次,你就给一条提示,提示你的朋友有几个数字是“数字和位置均正确”的(称为Bulls),有几个数字是“数字正确但位置不正确”的(称为Cows)。你的朋友要根据这些提示猜出正确的数字。

比如,给出的数字是1807,你的朋友猜的是7810,那么你要给他的提示是“1个Bulls,3个Cows”。
你要写一个函数来生成提示,如果用A代表Bulls,B代表Cows,那么上面的例子输出应该是“1A3B”。

注意,secret(被猜测数字)和guess(猜测的数字)都可能包含重复数字。比如,给出数字是1123,猜的数字是0111,那么你的函数应该返回“1A1B”,因为第一个数字1是Bulls,第二个或第三个数字1是Cows。

假定 secret 和 guess 均只包含数字,且长度总是是一样的。

思路方法

这个题目一定要首先弄清题目的意思,主要有两点要注意:1,每次给出的数字可能是重复的(跟一般猜数字不同);2,题目没说是每次四个数字,只说secret和guess一样长。

思路一

比较直观的想法是,按照A和B的定义,分别计算出它们的数目即可。注意在计算B时,要避免重复计数。

代码

class Solution(object):    def getHint(self, secret, guess):        """        :type secret: str        :type guess: str        :rtype: str        """        secret = list(secret)        guess = list(guess)        A = B = 0        # 求A        i = 0        while (i < len(secret)):            if secret[i] == guess[i]:                A += 1                del secret[i]                del guess[i]            else:                i += 1        # 求B        for c in secret:            if c in guess:                guess.remove(c)                B += 1        return str(A) + 'A' + str(B) + 'B'

思路二

为了求A的值,要对数字串进行一遍遍历,实际上这个过程中可以顺便求B的值。只需要有一个长度为10的数组,每次记录0-9各个数字在secret和guess中的出现情况即可。

代码一

class Solution(object):    def getHint(self, secret, guess):        """        :type secret: str        :type guess: str        :rtype: str        """        A = B = 0        digits1 = [0]*10        digits2 = [0]*10        for i in xrange(0,len(secret)):            if secret[i] == guess[i]:                A += 1            else:                digits1[int(secret[i])] += 1                digits2[int(guess[i])] += 1        for i in range(0,10):            B += min(digits1[i], digits2[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        digits = [0]*10        for i in xrange(0,len(secret)):            if secret[i] == guess[i]:                A += 1            else:                digits[int(secret[i])] += 1                if digits[int(secret[i])] <= 0:                    B += 1                digits[int(guess[i])] -= 1                if digits[int(guess[i])] >= 0:                    B += 1        return str(A) + 'A' + str(B) + 'B'

PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.csdn.net/coder_orz/article/details/51314731

1 0