[LeetCode]Bulls and Cows | 书影博客



[LeetCode]Bulls and Cows | 书影博客

You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend guesses a number, you give a hint, the hint tells your friend how many digits are in the correct positions (called "bulls") and how many digits are in the wrong positions (called "cows"), your friend will use those hints to find out 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.)

According to Wikipedia: "Bulls and Cows (also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots) is an old code-breaking mind or paper and pencil game for two or more players, predating the similar commercially marketed board game Mastermind. The numerical version of the game is usually played with 4 digits, but can also be played with 3 or any other number of digits."

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.

You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.

题目大意:

你和朋友在玩下面的猜数字游戏(Bulls and Cows):你写下一个4位数的神秘数字然后让朋友来猜,你的朋友每次猜一个数字,你给一个提示,告诉他有多少个数字处在正确的位置上(称为"bulls" 公牛),以及有多少个数字处在错误的位置上(称为"cows" 奶牛),你的朋友使用这些提示找出那个神秘数字。

例如:

神秘数字:  1807 朋友猜测:  7810

提示信息:  1公牛 3奶牛。(公牛是8, 奶牛是0, 1和7)

根据维基百科:"公牛和奶牛(也称为奶牛和公牛,或者猪和公牛, 或者公牛和Cleots)" 是一�L古老的两人或多人参与的密码破解智力游戏或纸笔游戏,早于与之类似的市售棋牌游戏Mastermind。这款游戏的数字版本通常包含4位数,但也可以是3位或者其他任何位数。"

编写函数,根据神秘数字与朋友的猜测,返回一个提示信息,使用A表示公牛,B表示母牛,在上例中,你的函数应当返回1A3B。

你可以假设神秘数字和你朋友的猜测只包含数字,并且长度一定相等。

解题思路:

bull = secret与guess下标与数值均相同的数字个数

cow = secret与guess中出现数字的公共部分 - bull


Read full article from [LeetCode]Bulls and Cows | 书影博客


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts