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.

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.

Please note that both secret number and friend's guess may contain duplicate digits.

Example 1:

  1. Input: secret = "1807", guess = "7810"
  2.  
  3. Output: "1A3B"
  4.  
  5. Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.

Example 2:

  1. Input: secret = "1123", guess = "0111"
  2.  
  3. Output: "1A1B"
  4.  
  5. Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow.

题意:

猜字游戏。

我负责猜,

你负责给提示:数字和位置都对,bulls++。数字对位置不对,cows++。

思路:

挨个扫字符串s,

挨个扫字符串g

若s当前字符等于g的字符,bulls++

用int[] map = new int[256]建一个简化版的HashMap

思路类似valid anagram

s当前字符在map里标记为++

p当前字符在map里标记为--

那么,若s当前字符已经被标记为--,说明p的字符来标记过,即它们字符相同但位置不同,cow++。

同理,若p当前字符已经被标记为++, 说明s的字符来标记过, 即它们字符相同但位置不同,cow++。

代码:

  1. class Solution {
  2. public String getHint(String secret, String guess) {
  3. // corner
  4. if(secret.length() != guess.length()) return false;
  5. int[] map = new int[256];
  6. int bull = 0;
  7. int cow = 0;
  8. for(int i = 0; i < secret.length();i++){
  9. char s = secret.charAt(i);
  10. char g = guess.charAt(i);
  11. if(s == g){
  12. bull++;
  13. }else{
  14. if(map[s]<0) cow++;
  15. if(map[g]>0) cow++;
  16. map[s]++;
  17. map[g]--;
  18. }
  19. }
  20. return bull +"A" + cow + "B";
  21. }
  22. }

[leetcode]299. Bulls and Cows公牛和母牛的更多相关文章

  1. LeetCode OJ:Bulls and Cows (公牛与母牛)

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

  2. LeetCode 299 Bulls and Cows

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

  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. 【一天一道LeetCode】#299. Bulls and Cows

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...

  7. 299. Bulls and Cows

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

  8. 299 Bulls and Cows 猜数字游戏

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

  9. Java [Leetcode 229]Bulls and Cows

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

随机推荐

  1. [UE4GamePlay架构(九)GameInstance(转)

    GameInstance这个类可以跨关卡存在,它不会因为切换关卡或者切换游戏模式而被销毁.然而,GameMode和PlayController就会再切换关卡或者游戏模式时被引擎销毁重置,这样他们里面的 ...

  2. 由一条普通的link引用引发的无数问号,大家能回答的帮忙回答回答吧.

    <link type="text/css" rel="stylesheet" href="1.css" /> 对于前台工作者来说 ...

  3. windows下面安装easy_install和pip教程

    方便安装whl:安装完成后,可以使用pip install   xxx.whl 安装一个python轮子 python扩展库的路径:Python\Python36\Lib\site-packages\ ...

  4. delphi WebBrowser的使用方法详解(五)-难点释疑

    网页代码:<SELECT id=fy onchange=TouchRefresh(1) name=fy> <OPTION selected value=15>每頁顯示15筆&l ...

  5. Executor框架(二)Executor 与 ExecutorService两个基本接口

    一.Executor 接口简介 Executor接口是Executor框架的一个最基本的接口,Executor框架的大部分类都直接或间接地实现了此接口. 只有一个方法 void execute(Run ...

  6. linux操作系统4 软件包管理

    知识内容: 1.软件包介绍 2.基本软件包安装 3.yum软件包管理 4.apt软件包管理 5.源码安装 一.软件包介绍 1.软件包分类 压缩包形式:类似.tar.gz结尾的文件(源码) rpm: r ...

  7. python 字符串与字节之间的相互转化

    1.将字符串转化成字节 b'fffff' bytes('ffff', encoding='utf-8') 'ffff'.encode('utf-8') 2.将字节转化成字符串 str(data, en ...

  8. HAproxy使用

    参考官网 安装HAproxy/ pull 官方镜像 本地安装:本地安装路径:/usr/local/haproxy/配置: 添加:/usr/local/haproxy/conf/haproxy.cfg添 ...

  9. openStack虚拟机error 错误状态基于差异镜像+基镜像做恢复

  10. 53. sql2005“备份集中的数据库备份与现有的xx数据库不同”解决方法

    RESTORE DATABASE LiveBOS_KeJiFROM DISK = 'D:\LiveBOS_KeJi_backup_201503090000.bak' --bak文件路径with rep ...