一天一道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)

先统计每个数出现

具体思路见代码注释:

  1. class Solution {
  2. public:
  3. string getHint(string secret, string guess) {
  4. int hash[10] = {0};//0~9在secret出现的次数
  5. int len = secret.length();
  6. int countA = 0 ,countB=0;
  7. int *isFind = new int[len];//统计那些位上的数相同
  8. memset(isFind,0,len*sizeof(int));
  9. for(int i =0 ; i < len ;i++)
  10. {
  11. if(secret[i]==guess[i]){//相同位上有相同数
  12. isFind[i] = 1;
  13. countA++;
  14. }
  15. else hash[secret[i]-'0']++;//如果相同位上数不相等就记录下来
  16. }
  17. for(int i =0 ; i < len ;i++)
  18. {
  19. if(isFind[i]==0){//跳过相同位上有相同数
  20. if(hash[guess[i]-'0']!=0)//如果出现过
  21. {
  22. hash[guess[i]-'0']--;//次数减1
  23. countB++;
  24. }
  25. }
  26. }
  27. string ret;
  28. stringstream ss;
  29. ss<<countA;
  30. ss<<'A';
  31. ss<<countB;
  32. ss<<'B';
  33. ss>>ret;//按特定格式输出
  34. return ret;
  35. }
  36. };

【一天一道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. ●BZOJ 3129 [Sdoi2013]方程

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3129 题解: 容斥,扩展Lucas,中国剩余定理 先看看不管限制,只需要每个位置都是正整数时 ...

  2. 【分解爪UVA11396-二分图染色模板】

    ·Rujia:"稍加推理即可解决该题--" ·英文题,述大意:      一张无向连通图,每个点连有三条边.询问是否可以将这个图分成若干个爪子,并满足条件:①每条边只能属于一个爪子 ...

  3. Angular中 build的时候遇到的错误--There are multiple modules with names that only differ in casing

    今天早上遇到一个Angular的编译的时候的错误 具体信息: There are multiple modules with names that only differ in casing.This ...

  4. java集合之HashMap源码解读

    源自:jdk1.8.0_121 HashMap继承自AbstractMap,实现了Map.Cloneable.Serializable. HashMap内部是由数组.链表.红黑树实现的 变量 // 默 ...

  5. bootstrap的模态框

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. HTMLParser使用简介

    HTMLParser具有小巧,快速的优点,缺点是相关文档比较少(英文的也少),很多功能需要自己摸索.对于初学者还是要费一些功夫的,而一旦上手以后,会发现HTMLParser的结构设计很巧妙,非常实用, ...

  7. ubuntu15.10 opencv3.1 安装配置codeblocks

    安装codeblocks: sudo add-apt-repository ppa:damien-moore/codeblocks-stable // 添加codeblocks的ppa sudo ap ...

  8. centos 7 破解密码

    CentOS 7 root密码的重置方式和CentOS 6完全不一样,CentOS 7与之前的版本6变化还是比较大的,以进入单用户模式修改root密码为例. 1.重启开机按esc   2.按e     ...

  9. Go 语言教程

    Go 语言教程 Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发, ...

  10. java开源即时通讯软件服务端openfire源码构建

    java开源即时通讯软件服务端openfire源码构建 本文使用最新的openfire主干代码为例,讲解了如何搭建一个openfire开源开发环境,正在实现自己写java聊天软件: 编译环境搭建 调试 ...