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 };
  • 相关阅读:
    夫妻合伙创业,度过艰苦的时期,酒楼生意蒸蒸日上
    搬运工做起了石头生意,他的石雕产品深受喜爱
    都市白领开毛衣店,仅一年时间收入20万元
    高中生下海创业,几十年来他始终将家乡的利益放在首位
    白手起家的他,创立公司,身价达到数千万
    从搬砖工到亿万富豪,这些年他经历了什么?
    货车司机创业做物流,他只为赢得老百姓的口碑
    患难夫妻做服装,他们的公司在全国做出了名堂
    struts2注解的作用
    struts2注解的作用
  • 原文地址:https://www.cnblogs.com/Deribs4/p/5734313.html
Copyright © 2011-2022 走看看