1. 程式人生 > >(LeetCode)Bulls and Cows --- 數字匹配問題

(LeetCode)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 01 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.

Credits:
Special thanks to @jeantimex for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

解題分析:

     對於此題的理解是這樣的,首選給出兩個字串,一個是secret   一個是guess,

然後分別按著 secert 的每一位去對比guess對應位置,若對應位置的元素值相同,則我們在bull上加一,如果沒有的話我們在cow上加一(注:此處需要將重複的數字去重,多個重複的我們只算一次)。

   方法,首先選出對應位置的加在bull,然後在剩餘的數字中去重,最後加到cow中。

# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
    def getHint(self, secret, guess):
        digit = {}
        bull = 0
        for i in xrange(len(secret)):
            if guess[i] == secret[i]:
                bull += 1
            else:
                if guess[i] not in digit:
                    digit[guess[i]] = 1
                else:
                    digit[guess[i]] += 1
        cow = 0
        for i in xrange(len(secret)):
            if guess[i] != secret[i] and secret[i] in digit and digit[secret[i]]>0:
               digit[secret[i]] -= 1
               cow += 1
        return str(bull) + 'A' + str(cow) + 'B'


相關推薦

LeetCodeBulls 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 numbe

[leetcode-299-Bulls and Cows]

!= solution exactly span str http ron discuss map You are playing the following Bulls and Cows game with your friend: You write down a nu

leetcode:(299) Bulls and Cows(java)

題目: 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. Eac

Leetcode 299 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.

LeetCode.299 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 y

leetcode筆記:Bulls and Cows

class app dup javascrip b+ any 猜數字 == always 一. 題目描寫敘述 You are playing the following Bulls and Cows game with your friend: You

領釦LeetCode數字轉換為十六進位制數 個人題解

給定一個整數,編寫一個演算法將這個數轉換為十六進位制數。對於負整數,我們通常使用 補碼運算 方法。 注意: 十六進位制中所有字母(a-f)都必須是小寫。 十六進位制字串中不能包含多餘的前導零。如果要轉化的數為0,那麼以單個字元'0'來表示;對於其他情況,十六進位制字串中的第一

leetcode 44 ---- 動態規劃困難 :萬用字元匹配java

1.  問題:給定一個字串 (s) 和一個字元模式 (p) ,實現一個支援 '?' 和 '*' 的萬用字元匹配。 '?' 可以匹配任何單個字元。 '*' 可以匹配任意字串(包括空字串)。 兩個字串完全匹配才算匹配成功。 說明: s 可能為空,且只包含從 a-z 的小

LeetCode】771JavaJewels and Stones

    You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in S 

數字的補數LeetCode

給定一個正整數,輸出它的補數。補數是對該數的二進位制表示取反。 注意: 給定的整數保證在32位帶符號整數的範圍內。 你可以假定二進位制數不包含前導零位。 示例 1: 輸入: 5 輸出: 2 解釋: 5的二進位制表示為101(沒有前導零位),其補數為010。所以你需要輸

LeetCode筆記:299. 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 wh

153. Find Minimum in Rotated Sorted Array旋轉數組的最小數字leetcode

sorted order 最小數 技術 變種 and 分法 目標 asc Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e

力扣LeetCode476. 數字的補數

它的 數字 bubuko lee 分享 leetcode inf time com 給定一個正整數,輸出它的補數。補數是對該數的二進制表示取反。 註意: 給定的整數保證在32位帶符號整數的範圍內。 你可以假定二進制數不包含前導零位。 示例 1: 輸入: 5 輸出: 2 解釋

100. Same TreeLeetCode

write end nodes ons code logs 相等 same tree treenode Given two binary trees, write a function to check if they are equal or not. Two binar

LeetCode兩個鏈表的第一個公共節點

struct link note lee mono lengthb borde san length LeetCode上面的題目例如以下: Write a program to find the node at which the intersection of t

191. Number of 1 BitsLeetCode

fun 其他 represent num span take style bin eight Write a function that takes an unsigned integer and returns the number of ’1‘ bits it has

342. Power of FourLeetCode

while sign its integer write return lee color style Given an integer (signed 32 bits), write a function to check whether it is a power of

項目優化經驗分享數據自己主動匹配

als 主動 options option reg shee total tomat 功能 從今天開始。我將和大家分享一下近期經手項目的優化經驗。今天我們分享的內容是:自己主動匹配! 引言: 輸入框數據自己主動匹配大家應該非常熟悉,當我們在使用百度或go

數據清洗小記2:全角數字轉換半角數字

大局觀 山東 轉載 亞洲 pop rpm包 stats mod 法律 原創作品。出自 “深藍的blog” 博客,歡迎轉載,轉載時請務必註明出處,否則有權追究版權法律責任。深藍的blog:http://blog.csdn.net/huangyanlong/article/d

205. Isomorphic StringsLeetCode

!= iso assume all lee span replace sam term Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if th