- 浏览: 174714 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 229Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 232You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 348Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 342Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 461Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 525Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 435Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 624Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 435The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 392Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 522Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 542Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 379All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 870Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 885Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 560Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 612Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 771Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 741You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 635For a undirected graph with tre ...
相关推荐
Bulls and Cows,在linux下编译成功,完整程序
Bulls 和 cows 有两种模式供您选择:使用字母和数字。您的任务是在有限次数的尝试中猜出单词或数字
公牛和牛 公牛和牛游戏说明: 每个游戏都以随机代码和4种不同的颜色开始,玩家的目标是最多显示7个回合。 玩家每转一圈都会猜出4种颜色的代码,按下“提交”按钮时,玩家会得到提示,提示有错误。...
这是一个互动式的网页,使用HTML , CSS和JavaScript制作而成,是原始的Bulls and Cows游戏的数字版本。 使用的工具和语言: 下载和用法: 可以从github Web界面将代码下载为压缩的zip文件。 还可以使用以下方法...
带有排行榜的 Bulls and Cows 游戏的 node.js 实现 分叉了优秀的: : 安装 下载并安装 node.js 和 npm 和 mongodb 跑步 转到您将克隆此 repo 的目录并运行: git clone ...
cows_and_bulls 经典的牛与牛游戏。 现场演示: : Bulls and Cows(也称为Cows and Bulls或Pigs and Bulls)是供两个或两个以上玩家使用的打破常规的思维或纸和铅笔游戏,早于商业发行的棋盘游戏Mastermind。
只是公牛和牛Just Bulls&Cows是一个简单的逻辑游戏。 游戏的目的是通过最少的尝试来猜测秘密数字。 演示:
Bulls-and-Cows-game
公牛和母牛 一款简单而有趣的安卓游戏。 该游戏基于 HTML5 和 jquery-mobile 。
matlab开发-CowsandBulls。该代码是一个幼稚的演绎游戏牛和公牛的实现,也称为moo。
公牛和牛 用虚幻引擎制作的游戏。
母牛和公牛 猜词游戏 奶牛的数量是在错误位置正确猜出的字母的数量。 多头的数量是正确位置上正确猜测的字母的数量。 在每一轮中,您都可以输入一个有效的英语单词作为猜测。 请记住,您的单词应该包含所有独特的...
leetcode卡公牛队和母牛队
bulls_and _cows.py
牛与牛Android中的代码破解思维游戏。要了解有关游戏及其玩法的更多信息: 先决条件Android SDK v27 Android构建工具v27.1.0 Android支持存储库v27.1.0运行应用本示例使用Gradle构建系统。 要构建此项目,请使用...
猜数字(又称 Bulls and Cows )是一种大概于20世纪中期兴起于英国的益智类小游戏。一般由两个人玩,也可以由一个人和电脑玩,在纸上、在网上都可以玩。这种游戏规则简单,但可以考验人的严谨和耐心。 标准规则 ...
bulls_and_cows
主要给大家介绍了关于如何利用Python开发一个简单的猜数字游戏的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
CowsAndBulls:公牛和牛游戏是为两个或多个玩家设计的密码破解游戏。 每个玩家都选择一个0到9之间4位数字的密码。数字必须全部不同。 游戏的目的是让每个玩家猜测另一个玩家的密码
leetcode 不会公牛和母牛-ghp 一个受 Leetcode 问题启发的简单想法。 查看如何玩(或阅读演示页面上的说明)。 请参阅现场演示。 一个示例游戏: 我的思考过程: 我从一个随机猜测开始。 我通常猜测不同的数字开始。...