摘要:题目这里主要是想记录一下这个很聪明的解法我规规矩矩的解法
题目:
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.
这里主要是想记录一下这个很聪明的解法:
public String getHint(String secret, String guess) { int[] nums = new int[10]; int countA = 0, countB = 0; for (int i = 0; i < secret.length(); i++) { int s = secret.charAt(i) - "0", g = guess.charAt(i) - "0"; if (s == g) { countA++; } else { if (nums[s] < 0) countB++; if (nums[g] > 0) countB++; nums[s]++; nums[g]--; } } return countA + "A" + countB + "B"; }
我规规矩矩的解法:
public String getHint(String secret, String guess) { Mapmap = new HashMap (); boolean[] visited = new boolean[guess.length()]; int bull = 0, cow = 0; for (char c : secret.toCharArray()) { if (map.containsKey(c)) { map.put(c, map.get(c) + 1); } else { map.put(c, 1); } } int len = Math.min(secret.length(), guess.length()); for (int i = 0; i < len; i++) { char c1 = secret.charAt(i), c2 = guess.charAt(i); if (c1 == c2) { bull++; visited[i] = true; map.put(c2, map.get(c2) - 1); if (map.get(c2) == 0) map.remove(c2); } } for (int i = 0; i < secret.length(); i++) { char c = guess.charAt(i); if (!visited[i]) { if (map.containsKey(c)) { cow++; visited[i] = true; map.put(c, map.get(c) - 1); if (map.get(c) == 0) map.remove(c); } } } return bull + "A" + cow + "B"; }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/64879.html
Problem 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 ...
摘要:题目要求游戏简单来说就是你随手写下一个位数,并让你同学猜这个数字是什么。第二次再在此基础上计算重合的值和没有重合的值的个数。这样的话,如果下一次遇到重复但是位置不同的值,我们可以知道它是否已经在中或是中出现过。 题目要求 You are playing the following Bulls and Cows game with your friend: You write down ...
摘要:已获原作者授权原系列地址游戏本章我们演示一个进阶例子我们用编写了游戏这个游戏也被称作或者或者是一个古老的益智解谜游戏由两名玩家参与早在世纪人们就在用铅笔和纸来玩这个游戏了在年发明的游戏正是受到这个游戏的启发和在基本理念上是一样的但被盒装出售 已获原作者授权. 原系列地址: Python Tkinter Mastermind 游戏 本章我们演示一个进阶例子. 我们用 Tkinter 编...
摘要:使用这么久对于数组的相关方法一直都是拿来就用对于方法更是常用。不过对于多个数组合并的时候因为返回的是新数组,可以链式下去。 使用JS这么久, 对于JS数组的相关方法一直都是拿来就用,对于push方法更是常用。但是在一次用到contact方法的时候自问了一句: push和contact到底有哪些区别? 先看下MDN的定义: 【push】:adds one or more element...
阅读 2938·2023-04-25 18:00
阅读 2184·2021-11-23 10:07
阅读 3980·2021-11-22 09:34
阅读 1114·2021-10-08 10:05
阅读 1535·2019-08-30 15:55
阅读 3309·2019-08-30 11:21
阅读 3262·2019-08-29 13:01
阅读 1323·2019-08-26 18:26