- 猜数字游戏
你在和朋友一起玩 猜数字(Bulls and Cows)游戏,该游戏规则如下:
写出一个秘密数字,并请朋友猜这个数字是多少。朋友每猜测一次,你就会给他一个包含下述信息的提示:
猜测数字中有多少位属于数字和确切位置都猜对了(称为 “Bulls”, 公牛),
有多少位属于数字猜对了但是位置不对(称为 “Cows”, 奶牛)。也就是说,这次猜测中有多少位非公牛数字可以通过重新排列转换成公牛数字。
给你一个秘密数字 secret 和朋友猜测的数字 guess ,请你返回对朋友这次猜测的提示。
提示的格式为 “xAyB” ,x 是公牛个数, y 是奶牛个数,A 表示公牛,B 表示奶牛。
请注意秘密数字和朋友猜测的数字都可能含有重复数字。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/bulls-and-cows
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法:1.使用2个hashmap,一个保存每个数字出现的次数,一个用来保存该位置是否为公牛;
2.遍历guess找公牛和奶牛
func getHint(secret string, guess string) (res string) {
hash_map := map[string]int{}
for _,char := range secret{
hash_map[string(char)]++
}
hash_map_loca := map[int]int{}
count_bull := 0
for i := 0; i < len(secret); i++{
if secret[i] == guess[i]{
count_bull++
hash_map[string(secret[i])]--
hash_map_loca[i]++
}
}
count_cows := 0
for i := 0; i < len(guess); i++{
if hash_map_loca[i] == 1{ //当前位置数字已经作为bull使用
continue
}
if hash_map[string(guess[i])] > 0{
hash_map[string(guess[i])]--
count_cows++
}
}
res = fmt.Sprintf("%dA%dB",count_bull,count_cows)
return
}