LeetCode 299 Bulls and Cows(公牛和母牛)(HashMap)

news/2024/10/17 16:29:47/

翻译

你在和朋友们玩一个叫做“公牛和母牛”的游戏:你写下一组数字,然后让你的朋友来猜它。每次你朋友做一个猜测,你根据他的猜测给一个提示:他在数字在值和位置上都猜对的数字,就叫做bulls(公牛),猜对了值但位置不对的数字叫做cows(母牛)。你的朋友将使用各种猜测和提示最终猜出来正确的数字。

例如:
你给的秘密数字是:“1807”
朋友的猜测是:“7810”

提示:1个公牛和3个母牛。(公牛是8,母牛是0、1、7。)

写一个函数用于根据你给的秘密数字和朋友的猜测做一个提示,使用A来表示公牛,使用B来表示母牛。在上面的例子中,你应该返回但是“1A3B”。

请注意,你给的秘密数字和朋友猜测的数字都可能包含重复的数字,例如:
你给的秘密数字是:“1123”
朋友的猜测是:“0111”

在这个情况下,朋友猜测的第一个1是公牛,第二个和第三个1是母牛,那么你的函数应该返回“1A1B”。

你可以假设秘密数字和猜测数字都只包含数字,并且它们的长度是相等的。

原文

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.

分析

需要注意的是上面的第二个例子,秘密数字是1123,猜测数字是0111,结果是1A1B,而不是1A2B,也就是说后面两个猜错位置但值正确的也只能算一个母牛。

也就是说,如果秘密数字是3456,猜测数字是2333,结果也应该是0A1B,而不是0A3B。

知道规则其实就很容易了,直接看代码吧,当然也可以先看看这些类似的问题~

LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

    public String getHint(String secret, String guess) {int bulls = 0, cows = 0;int[] s = new int[10], g = new int[10];for (int i = 0; i < secret.length(); i++) {char c1 = secret.charAt(i), c2 = guess.charAt(i);if (c1 == c2) {bulls++;} else {s[c1 - '0']++;g[c2 - '0']++;}}for (int i = 0; i < s.length; i++) {cows += Math.min(s[i], g[i]);}return bulls + "A" + cows + "B";}

http://www.ppmy.cn/news/206069.html

相关文章

1896. 公牛数学

题目描述 公牛在数学方面比奶牛强很多&#xff0c;他们自称可以计算很大的整数之间的乘法&#xff0c;并得到精确的结果。农夫约翰想知道他们的答案是否正确。请你帮助他检查公牛的答案。读入2个正整数&#xff08;不大于10^40&#xff09;&#xff0c;计算他们的乘积&#xf…

oppo手机android 版本号,oppo手机怎么升级到安卓11

oppo手机怎么升级到安卓11&#xff1f;在最近&#xff0c;谷歌发布了Android 11系统&#xff0c;国内很多品牌的手机也可以陆续可以升级到安卓11系统&#xff0c;那oppo手机怎么升级为安卓11系统呢&#xff0c;在哪里升级到安卓11&#xff0c;下面就和小编一起来看看吧&#xf…

oppor15android10怎么降级,OPPOR15系统降级教程

www.netded.com发表时间: 2018-11-05 10:45 也过来说说咱们的这个OPPO R15手机的系统降级操作了&#xff0c;也就是大家常说的回退操作了&#xff0c;因为有的机友感觉系统升级之后并不是特别满意&#xff0c;所以想进行系统回退操作&#xff0c;想回退到原来的旧版本的系统&am…

oracle+m10-5,pchm10是oppo什么型号

pchm10是oppo a11手机的型号。外观方面&#xff0c;oppo a11搭载6.5英寸的浅水滴阳光屏&#xff0c;拥有暮晨紫&#xff0c;极夜黑&#xff0c;流云白&#xff0c;湖光绿&#xff0c;香草薄荷5种配色。配置方面&#xff0c;oppo a11搭载骁龙665处理器&#xff0c;搭配5000mAh电…

android 判断oppo手机型号,oppo android 11支持什么型号

oppo android 11支持什么型号&#xff1f;在最近&#xff0c;谷歌和OPPO手机相继推出Android 11安卓系统版本&#xff0c;那OPPO手机哪些型号可以更新Android 11呢&#xff0c;oppo android 11支持什么型号&#xff0c;下面就和小编一起来看看吧&#xff01; Find X2系列可更新…

oppoa9处理器怎么样_oppoa9什么处理器 配置功能评测处理器型号是这个

oppoa9目前还处于预售阶段&#xff0c;是oppo手机继opporeno旗舰机发布之后&#xff0c;发布的一款中端机&#xff0c;关于oppoa9这款手机&#xff0c;大家对于新机的性能也是比较关注的&#xff0c;要想了解oppoa9的性能&#xff0c;还是要看新机的处理器。 在处理器方面&…

mac下adb找不到OPPO r11

背景 入职后&#xff0c;单位给我的测试手机是OPPO r11。结果手机连上mac电脑后&#xff0c;不管是Android Studio4.2还是adb&#xff0c;都找不到测试机。经过一番尝试&#xff0c;发现oppo需要特殊处理&#xff0c;详情参见本文内容 激活oppo r11 如果用的oppo r11是第一次…

oppo9s刷机教程_OPPOR9S刷机包

详情 OPPOR9S刷机包是专为OPPO手机刷机而推出的一款辅助工具&#xff0c;这款工具功能非常的强大&#xff0c;满足你的日常所需要解决的问题&#xff0c;OPPOR9S刷机包操作简单&#xff0c;几乎不需要太过复杂的操作只需照着点击即可&#xff0c;让您能够轻松上手&#xff0c;喜…