leetcode_299. Bulls and Cows 猜数字,返回猜对数字和猜对数字和位置的数字个数
来源:互联网 发布:人工智能的研究内容 编辑:程序博客网 时间:2024/05/22 08:01
题目:
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.
Credits:
Special thanks to @jeantimex for adding this problem and creating all test cases.
题意:
事先给定一个字符串,然后猜这个字符串,如果数字和位置都猜对,则算一次Bulls,如果只有数字猜对而位置不对,则算一次Cows。现在请写一个函数,根据给定的字符串secret和猜测的字符串guess,计算出Bulls次数和Cows次数。
代码:
class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
lens = len(secret)
if lens == 0 :
return '0A0B'
else :
secret_map = dict() #记录secret中各数字的个数
guess_map = dict() #记录guess中各数字的个数
bulls_count = dict() #记录数字和位置都猜对的数字个数
cows_count = dict() #记录只有数字猜对的数字个数
for i in range(lens) : #统计数字和位置都猜对的数字个数
if secret[i] == guess[i] :
if secret[i] not in bulls_count :
bulls_count[secret[i]] = 0
bulls_count[secret[i]] += 1
for x in secret : #统计secret中各数字出现次数
if x not in secret_map :
secret_map[x] = 0
secret_map[x] += 1
for x in guess : #统计guess中各数字出现次数
if x not in guess_map :
guess_map[x] = 0
guess_map[x] += 1
for x in guess_map : #统计只有数字猜对的数字个数
if x in secret_map :
temp = min(guess_map[x],secret_map[x]) #最小的那个才是猜对的
if x in bulls_count :
temp = temp - bulls_count[x] #减去位置也猜对了的次数,得到只有数字猜对的次数
cows_count[x] = temp
count1 = 0
count2 = 0
for x in bulls_count : #统计数字和位置都猜对的所有数字出现次数
count1 += bulls_count[x]
for x in cows_count : #统计只有数字猜对的所有数字出现次数
count2 += cows_count[x]
return str(count1)+'A'+str(count2)+'B'
笔记:
就是题目有点长,无其他难点。
- leetcode_299. Bulls and Cows 猜数字,返回猜对数字和猜对数字和位置的数字个数
- 299. Bulls and Cows ——猜数字游戏
- 数字和的个数
- 猜数字游戏(Bull and Cows)
- (LeetCode)Bulls and Cows --- 数字匹配问题
- reverse对数字和字符串的倒置
- 数字对
- 对数字的检测
- 对数字的操作
- 键盘输入字符串(含数字和字母),输出最长的数字子串和数字个数
- 猜数字 不使用重复的位置
- 所有数字和字母的个数!
- 输出字母数字和空格的个数。
- 猜数字
- 猜数字
- 猜数字
- 猜数字
- 猜数字
- 《五代史通俗演义》推荐——不良人粉丝的一点敬意.
- 数据结构学习笔记
- crontab
- jdbc访问数据库的步骤
- 如何一次改变word中的数字和字母的字体属性
- leetcode_299. Bulls and Cows 猜数字,返回猜对数字和猜对数字和位置的数字个数
- Laravel 的中大型專案架構
- 1037. Magic Coupon (25)
- jQuery笔记(一)
- IO复用之select分析笔记
- 最易懂的js原型之解析
- 16.11.3
- 周二
- LeetCode 41 First Missing Positive