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 };
  • 相关阅读:
    Intel x86
    FPGA自计数六位共阳极数码管动态显示2(调用task的方法)
    FPGA六位共阳极数码管动态显示
    运算放大器是模拟电路的基石,模拟电路设计
    这样讲你就懂了!大牛给你介绍《信号与系统》
    电容计算公式
    fork...join的用法
    芯片电源管脚的去耦电容究竟要用多大的?
    Blog Contents
    linux grep 命令常见用法
  • 原文地址:https://www.cnblogs.com/Deribs4/p/5734313.html
Copyright © 2011-2022 走看看