LeetCode 299. Bulls and Cows

20 篇文章 0 订阅

LeetCode 299. Bulls and Cows

题目描述

You are playing the Bulls and Cows game with your friend.

You write down a secret number and ask your friend to guess what the number is. When your friend makes a guess, you provide a hint with the following info:

The number of “bulls”, which are digits in the guess that are in the correct position.
The number of “cows”, which are digits in the guess that are in your secret number but are located in the wrong position. Specifically, the non-bull digits in the guess that could be rearranged such that they become bulls.
Given the secret number secret and your friend’s guess guess, return the hint for your friend’s guess.

The hint should be formatted as “xAyB”, where x is the number of bulls and y is the number of cows. Note that both secret and guess may contain duplicate digits.

思路

  1. 同时遍历secret和guess, 使用一个哈希表fre[byte]int来记录secret和guess中每个数字出现的次数
  2. 如果secret[i] == guess[i],则bulls++
  3. 否则, secret[i]出现的次数加1,guess[i]出现的次数减1
  4. 同时判断, secret[i]出现的次数是否小于0,因为如果小于0,说明之前出现过,在第3步中已经减过1了,所以如果小于0,说明guess[i]出现过,cows++
  5. 同理, guess[i]出现的次数是否大于0,如果是,则cows++
  6. 最后返回bulls和cows的字符串

例如: secret = “1123”, guess = “0111”

  1. secret[0] != guess[0], 因为fre[‘1’] = fre[‘0’] = 0, 所以不进入if, 最后fre中数据为: fre[‘1’] = 1, fre[‘0’] = -1, bulls = 0, cows = 0
  2. secret[1] == guess[1], 此时fre中数据为同上, bulls = 1, cows = 0
  3. 重点: secret[2] != guess[2], 因为fre[‘1’] = 1 > 0, 所以进入第二个if, cows++, 然后 fre[‘1’]–, fre[‘2’]++, 最后fre中数据为: fre[‘1’] = 0, fre[‘2’] = 1, bulls = 1, cows = 1
  4. secret[3] != guess[3], 此时fre[‘3’] = 0, fre[‘1’] = 0, 所以不进入任何if, 最后fre中数据为: fre[‘1’] = 0, fre[‘3’] = 1, bulls = 1, cows = 1

代码

func getHint(secret string, guess string) string {
    fre := make(map[byte]int, 10)
    bulls, cows := 0, 0
	
    for i := 0; i < len(secret); i++ {
        if secret[i] == guess[i] {
            bulls++
        } else {
            if fre[secret[i]] < 0 {
                cows++
            }
            if fre[guess[i]] > 0 {
                cows++
            }
            fre[secret[i]]++
            fre[guess[i]]--
        }
    }
    return fmt.Sprintf("%dA%dB", bulls, cows)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值