美文网首页
299. Bulls and Cows

299. Bulls and Cows

作者: menghui524 | 来源:发表于2017-10-19 15:32 被阅读0次

一遍过。

public class Solution {
    public string GetHint(string secret, string guess) {
        if(string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(secret) || secret.Length != guess.Length){
            return "0A0B";
        }
        int bullCount = 0;
        int cowCount = 0;
        var secretDict = new Dictionary<char, int>();
        var guessDict = new Dictionary<char, int>();
        for(int i = 0; i < secret.Length; i++){
            //exactly equal
            if(secret[i] == guess[i]){
                bullCount++;
            }
            else{
                if(!secretDict.ContainsKey(secret[i])){
                    secretDict[secret[i]] = 1;
                }
                else{
                    secretDict[secret[i]] += 1;
                }
                if(!guessDict.ContainsKey(guess[i])){
                    guessDict[guess[i]] = 1;
                }
                else{
                    guessDict[guess[i]] += 1;
                }
            }
        }
        foreach(char c in secretDict.Keys){
            int timeInSecret = secretDict[c];
            if(guessDict.ContainsKey(c)){
                int timeInGuess = guessDict[c];
                cowCount += Math.Min(timeInSecret, timeInGuess);
            }
        }
        return bullCount + "A" + cowCount + "B";
    }
}

相关文章

  • 2019-02-09

    LeetCode 299. Bulls and Cows Description You are playing ...

  • 299. Bulls and Cows

    Description You are playing the following Bulls and Cows ...

  • 299. Bulls and Cows

    果然昨天焦虑了,嗯 ,其实最然方法不一定好,但是理一理思路还是清晰的,首先扫描一遍secret 与对应guess位...

  • 299. Bulls and Cows

    一遍过。

  • 299. Bulls and Cows

    问题 You are playing the following Bulls and Cows game with...

  • 299. Bulls and Cows

    You are playing the following Bulls and Cows game with yo...

  • LeetCode*299. Bulls and Cows

    LeetCode题目链接 注意:凡是以英文出现的,都是题目提供的,包括答案代码里的前几行。 题目: You are...

  • 299. Bulls and Cows(easy)

    introduction 你正在和你的朋友玩 猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友...

  • 299. Bulls and Cows [Medium] 数组

    299. Bulls and Cows 刚开始没看懂题目,这个问题很简单的,就是要统计两个数组中,有哪些是位置对,...

  • Leetcode-Java(三十)

    299. Bulls and Cows 一开始我用的是HashSet保存两个字符串中出现过的数字但是没有匹配上的,...

网友评论

      本文标题:299. Bulls and Cows

      本文链接:https://www.haomeiwen.com/subject/hunuuxtx.html