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 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:
Input: secret = "1807", guess = "7810"
Output: "1A3B"
Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.
Example 2:
Input: secret = "1123", guess = "0111"
Output: "1A1B"
Explanation: The 1st 1 in friend"s guess is a bull, the 2nd or 3rd 1 is a cow.
Note: You may assume that the secret number and your friend"s guess only contain digits, and their lengths are always equal.
class Solution { public String getHint(String secret, String guess) { //pre-process: get bulls int bulls = 0, cows = 0, len = secret.length(); char[] s1 = secret.toCharArray(); char[] s2 = guess.toCharArray(); for (int i = 0; i < len; i++) { if (s1[i] == s2[i]) { bulls++; s1[i] = "#"; s2[i] = "#"; } } //get cows with a digit map int[] dict = new int[10]; for (int i = 0; i < len; i++) { if (s1[i] == "#") continue; else dict[s1[i]-"0"]++; } for (int i = 0; i < len; i++) { if (s2[i] == "#") continue; else { if (dict[s2[i]-"0"] > 0) { cows++; dict[s2[i]-"0"]--; } } } return bulls+"A"+cows+"B"; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/72521.html
摘要:題目要求游戲簡(jiǎn)單來(lái)說(shuō)就是你隨手寫下一個(gè)位數(shù),并讓你同學(xué)猜這個(gè)數(shù)字是什么。第二次再在此基礎(chǔ)上計(jì)算重合的值和沒(méi)有重合的值的個(gè)數(shù)。這樣的話,如果下一次遇到重復(fù)但是位置不同的值,我們可以知道它是否已經(jīng)在中或是中出現(xiàn)過(guò)。 題目要求 You are playing the following Bulls and Cows game with your friend: You write down ...
摘要:題目這里主要是想記錄一下這個(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...
摘要:已獲原作者授權(quán)原系列地址游戲本章我們演示一個(gè)進(jìn)階例子我們用編寫了游戲這個(gè)游戲也被稱作或者或者是一個(gè)古老的益智解謎游戲由兩名玩家參與早在世紀(jì)人們就在用鉛筆和紙來(lái)玩這個(gè)游戲了在年發(fā)明的游戲正是受到這個(gè)游戲的啟發(fā)和在基本理念上是一樣的但被盒裝出售 已獲原作者授權(quán). 原系列地址: Python Tkinter Mastermind 游戲 本章我們演示一個(gè)進(jìn)階例子. 我們用 Tkinter 編...
摘要:使用這么久對(duì)于數(shù)組的相關(guān)方法一直都是拿來(lái)就用對(duì)于方法更是常用。不過(guò)對(duì)于多個(gè)數(shù)組合并的時(shí)候因?yàn)榉祷氐氖切聰?shù)組,可以鏈?zhǔn)较氯ァ? 使用JS這么久, 對(duì)于JS數(shù)組的相關(guān)方法一直都是拿來(lái)就用,對(duì)于push方法更是常用。但是在一次用到contact方法的時(shí)候自問(wèn)了一句: push和contact到底有哪些區(qū)別? 先看下MDN的定義: 【push】:adds one or more element...
摘要:的模塊系統(tǒng)被設(shè)計(jì)成讓你可以一次性引入多個(gè)變量。動(dòng)態(tài)靜態(tài),或者說(shuō)規(guī)矩和如何打破規(guī)矩作為一門動(dòng)態(tài)編程語(yǔ)言,令人驚訝地?fù)碛幸粋€(gè)靜態(tài)的模塊系統(tǒng)。只要你的需求都是靜態(tài)的話,這個(gè)模塊系統(tǒng)還是很的。 此文為翻譯,原文地址在這兒:https://hacks.mozilla.org/2015/08/es6-in-depth-modules/ ES6 是 ECMAScript 第 6 版本的簡(jiǎn)稱,這是新一...
閱讀 1833·2021-11-25 09:43
閱讀 1335·2021-11-22 15:08
閱讀 3735·2021-11-22 09:34
閱讀 3225·2021-09-04 16:40
閱讀 3002·2021-09-04 16:40
閱讀 542·2019-08-30 15:54
閱讀 1335·2019-08-29 17:19
閱讀 1752·2019-08-28 18:13