一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

来源: https://leetcode.com/problems/bulls-and-cows/

(二)解题

题目大意:给定两个string变量a和b,bulls表示a和b相同位上有相同数的个数,cows表示a和b不同位上有相同数的个数。注意计算过的不能再算。

解题思路:分为两类来计算,如1807和7810

(1)相同位上有相同数(8,8)

直接统计这样的位数即可

(2)相同位上有不同数(107和710)

先统计每个数出现

具体思路见代码注释:

class Solution {
public:
    string getHint(string secret, string guess) {
        int hash[10] = {0};//0~9在secret出现的次数
        int len = secret.length();
        int countA = 0 ,countB=0;
        int *isFind = new int[len];//统计那些位上的数相同
        memset(isFind,0,len*sizeof(int));
        for(int i =0 ; i < len ;i++)
        {
            if(secret[i]==guess[i]){//相同位上有相同数
                isFind[i] = 1;
                countA++;
            }
            else hash[secret[i]-'0']++;//如果相同位上数不相等就记录下来
        }
        for(int i =0 ; i < len ;i++)
        {
            if(isFind[i]==0){//跳过相同位上有相同数
                if(hash[guess[i]-'0']!=0)//如果出现过
                {
                    hash[guess[i]-'0']--;//次数减1
                    countB++;
                }
            }
        }
        string ret;
        stringstream ss;
        ss<<countA;
        ss<<'A';
        ss<<countB;
        ss<<'B';
        ss>>ret;//按特定格式输出
        return ret;
    }
};

【一天一道LeetCode】#299. Bulls and Cows的更多相关文章

  1. LeetCode 299 Bulls and Cows

    Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...

  2. [leetcode]299. Bulls and Cows公牛和母牛

    You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...

  3. Leetcode 299 Bulls and Cows 字符串处理 统计

    A就是统计猜对的同位同字符的个数 B就是统计统计猜对的不同位同字符的个数 非常简单的题 class Solution { public: string getHint(string secret, s ...

  4. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  5. 299. Bulls and Cows - LeetCode

    Question 299. Bulls and Cows Solution 题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一 ...

  6. 299. Bulls and Cows

    题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...

  7. 299 Bulls and Cows 猜数字游戏

    你正在和你的朋友玩猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为”Bulls“, 公牛),有多少位数字 ...

  8. Java [Leetcode 229]Bulls and Cows

    题目描述: You are playing the following Bulls and Cows game with your friend: You write down a number an ...

  9. LeetCode(45)-Bulls and Cows

    题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...

随机推荐

  1. 2015 多校联赛 ——HDU5372(树状数组)

    Sample Input 3 0 0 0 3 0 1 5 0 1 0 0 1 1 0 1 0 0   Sample Output Case #1: 0 0 0 Case #2: 0 1 0 2 有0, ...

  2. hdu 4267 线段树间隔更新

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. [济南集训 2017] 求gcd之和

    题目大意: 求\(\sum_{i=1}^n\sum_{j=1}^mgcd(i,j)\) 解题报告: 有一个结论:一个数的所有因子的欧拉函数之和等于这个数本身 运用这个我们可以开始推: \(\sum_{ ...

  4. APIO2017 懵逼记

    Day -1: 移步http://www.cnblogs.com/juruolty/p/6854795.html Day 0: CTSC铁牌后,下一个就是APIO了. lmy,sxy,cxc,lh过来 ...

  5. C# 导入excel报错 :不是预期外部表

    错误原因:由于Excel 97-2003的连接格式与Excel 2010 的 不同造成. 解决方案1: 很多人换了2010后,问的最多的问题之一是2003里最经典的ADO中的“provider=Mic ...

  6. AspNetCoreApi 跨域处理

    AspNetCoreApi 跨域处理 如果咱们有处理过MV5 跨域问题这个问题也不大. (1)为什么会出现跨域问题:  浏览器安全限制了前端脚本跨站点的访问资源,所以在调用WebApi 接口时不能成功 ...

  7. jsvascript === 和==的区别

    ==   用于比较   判断 两者相等      ==在比较的时候可以转自动换数据类型 ===用于严格比较   判断两者严格相等     ===严格比较,不会进行自动转换,要求进行比较的操作数必须类型 ...

  8. Postgresql查询最近12个月、最近30天数据

    -- 最近 12 个月 SELECT * FROM 表名 WHERE 日期字段 BETWEEN (now() - INTERVAL '12 months') AND now() -- 最近 30 天 ...

  9. 零开始:NetCore项目权限管理系统:定义基本接口和实现

    上一篇讲了基础的框架搭建    地址:http://www.cnblogs.com/fuyu-blog/p/8909779.html 这篇主要讲解SqlSugar   ORM的数据库连接以及建表和接口 ...

  10. 读书笔记-《Maven实战》-关于Maven依赖传递的思考 2018/4/26

    上次读书笔记中,提到了依赖传递.看着依赖传递表,一直在思考为什么会是这样. 先看传递表: compile test provided runtime compile test provided run ...