摘要:題目這里主要是想記錄一下這個(gè)很聰明的解法我規(guī)規(guī)矩矩的解法
題目:
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.
這里主要是想記錄一下這個(gè)很聰明的解法:
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"; }
我規(guī)規(guī)矩矩的解法:
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"; }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/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 ...
摘要:題目要求游戲簡單來說就是你隨手寫下一個(gè)位數(shù),并讓你同學(xué)猜這個(gè)數(shù)字是什么。第二次再在此基礎(chǔ)上計(jì)算重合的值和沒有重合的值的個(gè)數(shù)。這樣的話,如果下一次遇到重復(fù)但是位置不同的值,我們可以知道它是否已經(jīng)在中或是中出現(xiàn)過。 題目要求 You are playing the following Bulls and Cows game with your friend: You write down ...
摘要:已獲原作者授權(quán)原系列地址游戲本章我們演示一個(gè)進(jìn)階例子我們用編寫了游戲這個(gè)游戲也被稱作或者或者是一個(gè)古老的益智解謎游戲由兩名玩家參與早在世紀(jì)人們就在用鉛筆和紙來玩這個(gè)游戲了在年發(fā)明的游戲正是受到這個(gè)游戲的啟發(fā)和在基本理念上是一樣的但被盒裝出售 已獲原作者授權(quán). 原系列地址: Python Tkinter Mastermind 游戲 本章我們演示一個(gè)進(jìn)階例子. 我們用 Tkinter 編...
摘要:使用這么久對于數(shù)組的相關(guān)方法一直都是拿來就用對于方法更是常用。不過對于多個(gè)數(shù)組合并的時(shí)候因?yàn)榉祷氐氖切聰?shù)組,可以鏈?zhǔn)较氯ァ? 使用JS這么久, 對于JS數(shù)組的相關(guān)方法一直都是拿來就用,對于push方法更是常用。但是在一次用到contact方法的時(shí)候自問了一句: push和contact到底有哪些區(qū)別? 先看下MDN的定義: 【push】:adds one or more element...
摘要:的模塊系統(tǒng)被設(shè)計(jì)成讓你可以一次性引入多個(gè)變量。動(dòng)態(tài)靜態(tài),或者說規(guī)矩和如何打破規(guī)矩作為一門動(dòng)態(tài)編程語言,令人驚訝地?fù)碛幸粋€(gè)靜態(tài)的模塊系統(tǒng)。只要你的需求都是靜態(tài)的話,這個(gè)模塊系統(tǒng)還是很的。 此文為翻譯,原文地址在這兒:https://hacks.mozilla.org/2015/08/es6-in-depth-modules/ ES6 是 ECMAScript 第 6 版本的簡稱,這是新一...
閱讀 3021·2023-04-25 18:00
閱讀 2222·2021-11-23 10:07
閱讀 4061·2021-11-22 09:34
閱讀 1250·2021-10-08 10:05
閱讀 1572·2019-08-30 15:55
閱讀 3435·2019-08-30 11:21
閱讀 3339·2019-08-29 13:01
閱讀 1378·2019-08-26 18:26