• Leetcode 299. Bulls and Cows


    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.(假设字符串长度相同,且只包含数字)

    题目大意:

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

    例如:

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

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

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

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

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

    思路:

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

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

    直接Hash解决。

     1 class Solution {
     2 public:
     3     string getHint(string secret, string guess) {
     4         int len = secret.length(), leng = guess.length(), bull = 0, cows = 0, a[10]={0}, i;
     5         string str = "";
     6         for(i = 0; i < len && i < leng; i++){
     7             a[secret[i] - '0']++;
     8             if(secret[i] == guess[i])
     9                 bull++;
    10         }
    11             
    12         for(i = 0; i < leng; i++){
    13             if(a[guess[i] - '0'] > 0){
    14                 cows++;
    15                 a[guess[i] -'0']--;
    16             }
    17         }
    18         cows = cows - bull;
    19         str = str + to_string(bull) + "A" + to_string(cows) + "B";
    20         return str;
    21     }
    22 };
  • 相关阅读:
    基因数据分析主流软件与基因预测方法步骤-搬运工
    调用约定__cdecl __fastcall与__stdcall
    const用法体会
    NS3系列——eclipse + NS3环境搭建
    小div在大div里面水平垂直都居中的实现方法
    template.js插件和ajax一起使用的例子
    js获取时间的函数集
    ajax获取json数据及实现跨域请求
    JavaScript里的Date 对象属性及对象方法--实现简单的日历
    css单行文本及多行文本溢出显示省略号
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/5729907.html
Copyright © 2020-2023  润新知