299. Bulls and Cows [easy] (Python)

7309 ワード

タイトルリンク
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):あなたは数字を書いて友达に当てさせます.あなたの友达は1回推測するたびに、あなたの友达にいくつかの数字が「数字と位置が正しい」(Bullsと呼ばれている)ことをヒントにして、いくつかの数字が「数字は正しいが位置が正しくない」(Cowsと呼ばれています).あなたの友达はこれらのヒントに基づいて正しい数字を推測しなければなりません.
例えば、与えられた数字は1807で、あなたの友达は7810だと推測して、それではあなたが彼にヒントを与えるのは“1つのBulls、3つのCows”です.AでBulls、BでCowsを表す場合、上記の例の出力は「1 A 3 B」です.
なお、secret(推測された数字)およびguess(推測された数字)は、重複した数字を含む可能性がある.例えば、与えられた数字は1123で、当てられた数字は0111で、あなたの関数は「1 A 1 B」を返すべきです.最初の数字1はBullsで、2番目または3番目の数字1はCowsです.
secretとguessはいずれも数字のみを含み、長さは常に同じであると仮定します.
考え方
この問題はまず問題の意味を明らかにしなければならない.主に2つの点に注意しなければならない.2,テーマは毎回4つの数字とは言わず、secretはguessと同じ長さしか言わない.
考え方1
比較的直感的な考え方は、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'

考え方2
Aの値を求めるために、数字列を一度遍歴しますが、実はこの過程でBの値を求めることができます.長さ10の配列が1つ必要で、secretとguessで0-9の各数字の出現状況を記録するたびに使用できます.
コード1
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'

上のコードは2つの追加配列を使用していますが、実際には1つの追加配列で実現することもできます.
コード2
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