博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【LeetCode】299 Bulls and Cows (java实现)
阅读量:6855 次
发布时间:2019-06-26

本文共 3724 字,大约阅读时间需要 12 分钟。

hot3.png

#原题: 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.

#题目要求:   该题要求比较清晰,给定一个数字,别人来猜。猜的数字和给定的数字进行比较,位数相同且对应位置数字也相同的,被称作bulls;数字相同但位数不同的叫做cows。要求就是给出bulls和cows的个数,方法结构如下,数字都以字符串的形式表现,返回值也是,其中,A表示bulls,B表示cows。

public String getHint(String secret, String guess)

  这里需要注意的是,如果bulls中已经出现的数字,就不能再算到cows中了,这就是上面的用例中,为什么会返回1A1B了。 ##常规解法   将secret和guess都解析成两个map,map的key就是位数,而value是位置上对应的数字。然后进行两轮轮训,第一轮找出bulls,然后从map中删除bulls;第二轮再找出cows,需要两次循环。   这种解法很常规,也很容易想到,但代码很臃肿,也没有什么新意。

public class Solution {    HashMap
getMap(String numString) { HashMap
map = new HashMap
(); int size = numString.length(); for (int i = 0; i < size; i++) { map.put(size -i, numString.charAt(i) - '0'); } return map; } public String getHint(String secret, String guess) { int cntA = 0; int cntB = 0; HashMap
mapSecret = getMap(secret); HashMap
mapGuess = getMap(guess); int cntSecret = mapSecret.size(); for (int i = 0; i < cntSecret; i++) { int bit = i + 1; if (!mapGuess.containsKey(bit)) { break; } if (mapSecret.get(bit) == mapGuess.get(bit)) { cntA++; mapSecret.remove(bit); mapGuess.remove(bit); } } for (int i = 0; i < cntSecret; i++) { int bit = i + 1; if (!mapSecret.containsKey(bit)) { continue; } for (Integer key : mapGuess.keySet()) { if (mapSecret.get(bit) == mapGuess.get(key)) { cntB++; mapGuess.remove(key); break; } } } String ret = String.format("%dA%dB", cntA, cntB); return ret; }}

  ps:我这里在代码中还验证了如果位数不相等的情况,但实际的用例中,好像并不用考虑这个。 ##更聪明的代码

public class Solution {    public String getHint(String secret, String guess) {        int bulls = 0;        int cows = 0;        int[] numbers = new int[10];        for (int i = 0; i
0) cows++; } } return bulls + "A" + cows + "B"; }}

  这个是discuss中的hot代码,真的是充满了智慧,尤其体现在处理cows的地方。创建了一个长度为10的int数组,因为每个位置的数字范围就是0——9,数组的index就对应0——9,而index对应的元素值表示secret中该index出现的次数。如果secret出现一次某个数字,该位置就自增1,如果此时该位置小于0,就说明这个数字在guess出现过,因此cows就自增1;guess的处理和secret类似,区别就是index对应元素需要自减。

转载于:https://my.oschina.net/styshoo/blog/660078

你可能感兴趣的文章
【学神】1-10 硬盘管理、文件系统及链接
查看>>
mvc与三层结构终极区别
查看>>
华为内部如何实施微服务架构?基本就靠这5大原则
查看>>
PC机声音图标为不可用(声音图标打叉)
查看>>
Lowest Common Ancestor of a Binary Tree Part
查看>>
ASP.NET 新增时多字段取值解决方案
查看>>
文字域替换
查看>>
springboot+vue的前后端分离与合并方案
查看>>
.net中使用存储过程output值和返回值
查看>>
2594. [WC2006]水管局长数据加强版【LCT+最小生成树】
查看>>
Day5-awk
查看>>
C++链式队列基本操作
查看>>
考试题解集合
查看>>
docker基础2
查看>>
走到哪里
查看>>
Docker 学习(一)
查看>>
Win10环境下Redis和Redis desktop manager 安装
查看>>
微信支付宝签约流程总结
查看>>
Spring WebSocket实现消息推送
查看>>
Percona 数据库
查看>>