题目:

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 01 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.

链接: http://leetcode.com/problems/bulls-and-cows/

题解:

公牛和奶牛游戏。使用HashMap存下来secret里的字符和count,然后同时遍历secret和guess就可以了。最后还要遍历一次map把多加的cow减掉。

Time Complexity - O(n), Space Complexity - O(n)

public class Solution {public String getHint(String secret, String guess) {if(secret == null || guess == null || secret.length() != guess.length()) {return "0A0B";}int bulls = 0, cows = 0;Map<Character, Integer> map = new HashMap<>();for(int i = 0; i < secret.length(); i++) {char c = secret.charAt(i);if(!map.containsKey(c)) {map.put(c, 1);    } else {map.put(c, map.get(c) + 1);}}for(int i = 0; i < secret.length(); i++) {char sChar = secret.charAt(i);char gChar = guess.charAt(i);if(sChar == gChar) {bulls++;map.put(gChar, map.get(gChar) - 1);} else if(map.containsKey(gChar)) {cows++;map.put(gChar, map.get(gChar) - 1);}}for(char c : map.keySet()) {if(map.get(c) < 0) {cows += map.get(c);}}return String.valueOf(bulls) + "A" + String.valueOf(cows) + "B";}
}

二刷:

主要参考了Discuss里面的解。

  1. 我们可以用一个数组来存bulls和cows。用s和g来表示数组的数字值
  2. 当 s = g时,我们找到了bull, bulls++
  3. 否则我们要看
    1. nums[g] > 0的话,说明当前guess的这个数字曾经出现在secret中,这是一个cow,我们cow++
    2. 我们也要看是否nums[s] < 0, 这个表明当前ssecret的数字曾经出现在guess中,这也是一个cow,我们还是cow++
    3. 我们用正数记录下nums[s]为bull的一个位置,nums[s]++,  我们也用负数记录下guess中出现过的数字,nums[g]--,

Java:

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {public String getHint(String secret, String guess) {if(secret == null || guess == null || secret.length() != guess.length()) {return "0A0B";}int[] nums = new int[10];for (int i = 0; i < secret.length(); i++) {int s = secret.charAt(i) - '0';int g = guess.charAt(i) - '0';if (s == g) {bulls++;} else {if (nums[s] < 0) {  // bulls can be counted as cowscows++;}if (nums[g] > 0) {  // found num but in diff positioncows++;}nums[s]++;nums[g]--;} }return bulls + "A" + cows + "B";}
}

三刷:

延续了二刷的解法。主要使用一个count[]数组保存之前出现过的secret digits和guess digits。当前sDigit == gDigit时,bulls增加。 否则, 当count[sDigit] < 0时,说明之前出现在guess里, 当count[gDigit] > 0时,说明之前出现在secret里,这两种情况都要分别增加cows。之后再记录i这个位置的改动count[sDigit]++, count[gDigit]--。最后返回结果。

Java:

public class Solution {public String getHint(String secret, String guess) {if (secret == null || guess == null || secret.length() != guess.length()) {return "0A0B";}int bulls = 0;int cows = 0;int[] count = new int[10];for (int i = 0; i < secret.length(); i++) {int sDigit = secret.charAt(i) - '0';int gDigit = guess.charAt(i) - '0';if (sDigit == gDigit) {bulls++;} else {if (count[sDigit] < 0) {cows++;}if (count[gDigit] > 0) {cows++;}}count[sDigit]++;count[gDigit]--;}return bulls + "A" + cows + "B";}
}

Update:

public class Solution {public String getHint(String secret, String guess) {if (secret == null || guess == null || secret.length() != guess.length()) return "0A0B";int bullsCount = 0, cowsCount = 0;int len = secret.length();int[] count = new int[10];for (int i = 0; i < len; i++) {int sc = secret.charAt(i) - '0';int gc = guess.charAt(i) - '0';if (sc == gc) {bullsCount++;} else {if (count[gc] > 0) cowsCount++;if (count[sc] < 0) cowsCount++;count[sc]++;count[gc]--;}}return bullsCount + "A" + cowsCount + "B";}
}

Reference:

https://leetcode.com/discuss/67031/one-pass-java-solution

299. Bulls and Cows相关推荐

  1. 299 Bulls and Cows 猜数字游戏

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

  2. LeetCode 299. Bulls and Cows(公牛和母牛)

    原题网址:https://leetcode.com/problems/bulls-and-cows/ You are playing the following Bulls and Cows game ...

  3. leetcode 299. Bulls and Cows | 299. 猜数字游戏(Java)

    题目 https://leetcode.com/problems/bulls-and-cows/ 题解 一个踩比赞多的题,给我的感觉是中规中矩没啥特点,不知道在考察什么.. 思路是,用数组维护一个 m ...

  4. LeetCode 299. Bulls and Cows

    secret和guess按位读入,如果相等bull就加1,不相等就在各自统计不同数字出现次数的数组里加1(s_map[i]指secret里数字i出现的次数).最后s_map和g_map存的是各自str ...

  5. 299. Bulls and Cows

    题目:https://leetcode.com/problems/bulls-and-cows/ 思路: 这个题我做了2h多,看了好久没有理解题意.其实是说guess里面数字和位置都对的个数,gues ...

  6. leetcode算法题--Bulls and Cows

    原题链接:https://leetcode.com/problems/bulls-and-cows/ string getHint(string secret, string guess) {int ...

  7. leetcode oj java Bulls and Cows

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

  8. 猜数字(Bulls and Cows)游戏

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

  9. 猜数字游戏Bulls and Cows的解法

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 猜数字游戏Bulls and Cows的解法 前言 一.游戏规则 二.电脑出题你来猜 1.引入库 2.生成随机四位数密码 3.接收玩家 ...

最新文章

  1. easyopen原理解析——不到100行代码实现一个最精简的easyopen
  2. fastjson判空_fastjson JSON 对象为空保留null
  3. js实现按下删除键清空文本框内容
  4. DHCP服务器-配置
  5. linux桥接设置静态,centos6.10 桥接模式下配置静态ip
  6. Spark 性能相关参数配置详解-压缩与序列化篇
  7. RenderSection
  8. 杂项:Java un
  9. C# winform post 请求指定 url
  10. python下载微信公众号文章_Python 抓取微信公众号文章
  11. 矩张量计算 matlab,用来计算广义相对论常用张量计算的Matlab程序
  12. html中使用js实现体彩11选5随机选号
  13. 谷歌成功利用一台 54 量子比特的量子计算机
  14. 使用python爬取股票基金的最新数据消息,并用Excel绘制树状图一目了然
  15. win10系统怎样安装/更新独立显卡驱动
  16. 靶场练习第十四天~vulnhub靶场之dc-6
  17. 操作系统接口之批处理作业
  18. QtQuick 移动端开发实战系列(7)_屏幕旋转实现(Android)
  19. 1:使用递归函数计算1到n之和
  20. JAVASE之多线程初识

热门文章

  1. linux下slow,慢查询日志的分析工具mysqlsla的使用
  2. jmeter脚本写个小demo(html论坛自动发帖、json龙果学院-前后端分离)
  3. 软件测试核心之用例设计
  4. 技巧 | 在R语言中使用高德地图的API进行地理/逆地理编码(地址与经纬度的相互转换)...
  5. sf | 空间矢量对象的“聚合”操作
  6. html图像排列代码,HTML图像(示例代码)
  7. 前端新人如何有效地提高自己
  8. 程序员锁死公司服务器,导致600万元资金打水漂。网友神回复
  9. 前端开发基础知识整理--web综合篇
  10. 有哪些必看的前端 JS 库?