zoukankan      html  css  js  c++  java
  • Leetcode 299. Bulls and Cows

    299. Bulls and Cows

    • Total Accepted: 37528
    • Total Submissions: 120055
    • Difficulty: Easy

    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.

    思路:方法一更为巧妙。具体见代码。

    代码:
    方法一:只遍历1遍。hashtab[i]>0意味着截至当前位置,i数字在secret中出现次数大于guess中出现的次数;hashtab[i]<0意味着截至当前位置,i数字在secret中出现次数小于guess中出现的次数。

     1 class Solution {
     2 public:
     3     string getHint(string secret, string guess) {
     4         unordered_map<char,int> hashtab;
     5         int bulls=0,cows=0,size=secret.size();
     6         for(int i=0;i<size;i++){
     7             if(secret[i]==guess[i]) bulls++;
     8             else{
     9                 if(hashtab[secret[i]]++<0) cows++;
    10                 if(hashtab[guess[i]]-->0) cows++;
    11             }
    12         }
    13         return to_string(bulls) + "A" + to_string(cows) + "B";
    14     }
    15 };

    方法二:遍历2遍,第1遍去除满足bulls条件的数,第2遍计算cows的个数。

     1 class Solution {
     2 public:
     3     string getHint(string secret, string guess) {
     4         unordered_map<char,int> hashtab;
     5         int bulls=0,cows=0,size=secret.size();
     6         for(int i=0;i<size;i++){
     7             if(secret[i]==guess[i]) bulls++;
     8             else hashtab[secret[i]]++;
     9         }
    10         for(int i=0;i<size;i++){
    11             if(secret[i]!=guess[i]&&hashtab[guess[i]]>0){
    12                 hashtab[guess[i]]--;
    13                 cows++;
    14             }
    15         }
    16         return to_string(bulls) + "A" + to_string(cows) + "B";
    17     }
    18 };
  • 相关阅读:
    10:简单密码
    08:字符替换
    07:配对碱基链
    05:输出亲朋字符串
    18:等差数列末项计算
    09:密码翻译
    用最通俗的话说23种设计模式之代理模式
    Android学习之 UI效果
    精确到时分秒的jQuery插件例子
    Eclipse 常用快捷键
  • 原文地址:https://www.cnblogs.com/Deribs4/p/5734313.html
Copyright © 2011-2022 走看看