- 浏览: 186340 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
创建两个变量A, B记录bulls和cows的个数,bulls的个数很简单,记录对应位置字符相等的个数。创建两个数组,保存对应位置不相等时的字符个数。最后累加两个数组中相同位置值小的元素就是B的值。代码如下:
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.
创建两个变量A, B记录bulls和cows的个数,bulls的个数很简单,记录对应位置字符相等的个数。创建两个数组,保存对应位置不相等时的字符个数。最后累加两个数组中相同位置值小的元素就是B的值。代码如下:
public class Solution { public String getHint(String secret, String guess) { int[] a = new int[10]; int[] b = new int[10]; int A = 0; int B = 0; for(int i = 0; i < secret.length(); i++) { if(secret.charAt(i) == guess.charAt(i)) { A ++; } else { a[secret.charAt(i) - '0'] ++; b[guess.charAt(i) - '0'] ++; } } for(int i = 0; i < a.length; i++) { B += Math.min(a[i], b[i]); } String s = A + "A" + B + "B"; return s; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 273Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 277You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 395Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 385Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 509Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 576Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 489Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 678Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 482The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 439Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 590Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 603Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 435All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 913Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 941Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 612Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 707Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 875Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 801You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 741For a undirected graph with tre ...
相关推荐
Bulls and Cows,在linux下编译成功,完整程序
《Bulls and Cows:一个开源的逻辑游戏解析》 "Bulls and Cows"是一款深受玩家喜爱的逻辑推理游戏,其玩法分为字母模式和数字模式。这款游戏的核心在于挑战玩家的推理能力和策略运用,通过有限的猜测次数来正确猜出...
Bulls and Cows 游戏,又称为“猜数字”游戏,是一种经典的纸笔游戏,玩家需要通过提示猜出对方心中设定的一个秘密数字。在现代科技的发展下,这种游戏也被搬到了网页平台上,通过 HTML(超文本标记语言)进行实现。...
公牛和牛 公牛和牛游戏说明: 每个游戏都以随机代码和4种不同的颜色开始,玩家的目标是最多显示7个回合。 玩家每转一圈都会猜出4种颜色的代码,按下“提交”按钮时,玩家会得到提示,提示有错误。...
这是一个互动式的网页,使用HTML , CSS和JavaScript制作而成,是原始的Bulls and Cows游戏的数字版本。 使用的工具和语言: 下载和用法: 可以从github Web界面将代码下载为压缩的zip文件。 还可以使用以下方法...
《LeetCode卡:公牛和母牛(Bulls and Cows)详解》 在编程的世界里,LeetCode是一个广受欢迎的在线平台,它提供了一系列的编程挑战,旨在帮助开发者提升算法技能和解决问题的能力。其中,“Bulls and Cows”(简称B...
“公牛和牛”(Bulls and Cows)是一款经典的逻辑猜数字游戏,玩家需要在有限的尝试次数内猜出系统预设的秘密数字。这款游戏简单易懂,但却能锻炼玩家的逻辑推理能力。现在,我们将深入探讨一个使用Kotlin编程语言...
带有排行榜的 Bulls and Cows 游戏的 node.js 实现 分叉了优秀的: : 安装 下载并安装 node.js 和 npm 和 mongodb 跑步 转到您将克隆此 repo 的目录并运行: git clone ...
`BullsAndCows-master`这个文件名可能表示的是项目源码的主分支,包含游戏的全部源代码和资源。开发者可以深入研究这些文件,学习如何利用虚幻引擎4和C++构建类似的游戏。 总之,《公牛和母牛》游戏的开发过程涉及...
cows_and_bulls 经典的牛与牛游戏。 现场演示: : Bulls and Cows(也称为Cows and Bulls或Pigs and Bulls)是供两个或两个以上玩家使用的打破常规的思维或纸和铅笔游戏,早于商业发行的棋盘游戏Mastermind。
《MATLAB开发:Cows and Bulls游戏解析》 在MATLAB编程环境中,Cows and Bulls游戏,也称为“牛牛”或“哞”,是一种趣味性的数字猜测游戏。它旨在提高玩家的逻辑推理能力和数字敏感度。这个游戏的核心是通过算法...
在"bulls-and-cows-master"这个压缩包中,可能包含了游戏的所有源代码、资源文件、配置文件等。源代码文件可能包含HTML文件(用于布局和结构)、CSS文件(用于样式和布局)、JavaScript文件(用于游戏逻辑)以及可能...
母牛和公牛 猜词游戏 奶牛的数量是在错误位置正确猜出的字母的数量。 多头的数量是正确位置上正确猜测的字母的数量。 在每一轮中,您都可以输入一个有效的英语单词作为猜测。 请记住,您的单词应该包含所有独特的...
公牛和奶牛(Bulls and Cows)是一款经典的数字猜谜游戏,源自于英国的"Codebreakers"和美国的"Mastermind"。在这个游戏中,玩家需要猜测电脑生成的一个秘密数字,通过提示判断猜测的数字与目标数字的正确位置和正确...
bulls_and _cows.py
"公牛与母牛"(Bulls and Cows)是一款历史悠久的数字猜谜游戏,玩家需要猜测一个由若干位数字组成的目标数,每次猜测后,游戏会告知猜测中有多少数字位置正确但数字错误(称为“母牛”),有多少数字完全正确(称为...
牛与牛Android中的代码破解思维游戏。要了解有关游戏及其玩法的更多信息: 先决条件Android SDK v27 Android构建工具v27.1.0 Android支持存储库v27.1.0运行应用本示例使用Gradle构建系统。 要构建此项目,请使用...
本项目是一个名为“BullsAndCows”的猜数小游戏,它通过Java实现,并配备了一个图形用户界面(GUI)。本文将深入解析这个游戏的实现原理、技术选型以及关键代码,旨在为初学者提供一个学习和实践Java GUI编程的实例...
"bulls_and_cows"是一款经典的猜数字游戏,也被称为“密电码”或“猜数字”。在这个游戏中,玩家需要猜测一个预设的隐藏数字,系统会根据猜测给出反馈,告诉玩家正确数字中有多少个数字位置正确("公牛",Bulls)...
“公牛和牛”(Bulls and Cows)是一款经典的数字猜谜游戏,玩家需要根据系统生成的一个秘密数字,通过提示猜出这个数字。每当你猜测一个数字时,系统会告诉你猜对了多少位数,以及这些数字在正确位置上的数量。这个...