LeetCode_299. Bulls and Cows
299. Bulls and Cows
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:1bull and3cows. The bull is8, the cows are0,1and7.
Example 2:
Input: secret = "1123", guess = "0111" Output: "1A1B" Explanation: The 1st1in friend's guess is a bull, the 2nd or 3rd1is a cow.
Note: You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.
package leetcode.easy;
public class BullsAndCows {
public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] secretCounts = new int[10];
int[] guessCounts = new int[10];
for (int i = 0; i < guess.length(); i++) {
char s = secret.charAt(i);
char g = guess.charAt(i);
if (s == g) {
bulls++;
} else {
secretCounts[Character.getNumericValue(s)]++;
guessCounts[Character.getNumericValue(g)]++;
}
}
for (int i = 0; i < 10; i++) {
cows = cows + Math.min(secretCounts[i], guessCounts[i]);
}
String res = bulls + "A" + cows + "B";
return res;
}
@org.junit.Test
public void test() {
String secret1 = "1807";
String guess1 = "7810";
String secret2 = "1123";
String guess2 = "0111";
System.out.println(getHint(secret1, guess1));
System.out.println(getHint(secret2, guess2));
}
}
LeetCode_299. Bulls and Cows的更多相关文章
- [LeetCode] Bulls and Cows 公母牛游戏
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- LeetCode 299 Bulls and Cows
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- [Leetcode] Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- 299. Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- Java [Leetcode 229]Bulls and Cows
题目描述: You are playing the following Bulls and Cows game with your friend: You write down a number an ...
- [LeetCode299]Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- Bulls and Cows leetcode
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- 【一天一道LeetCode】#299. Bulls and Cows
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
随机推荐
- webpack打包绝对路径引用资源和element ui字体图标不显示的解决办法
webpack打包绝对路径引用资源解决办法: 打开webpack.prod.conf.js 找到output:增加 publicPath: './', 即可,如图 element ui字体图标不显 ...
- winform DateTimePicker 设置成秒
C# Windows窗体应用中,用到时间选择控件DateTimePicker,发现不能选择时分秒,难道要自己写一个控件?! 答案是否定的,通过属性修改是可以选择时间的,DateTimePicker完全 ...
- C语言函数的定义和使用(2)
一:无参函数 类型说明符 get(){ //函数体 } 二:无参函数 类型说明符 getname(int a,int b){ //函数体 } 三:类型说明符包括: int ,char,float,do ...
- 教你用WordPress搭建个人博客
1. 购买VPS,推荐几个供应商: 国外的有:搬瓦工 VirMach vps.net vultr.com 等等 国内的有:阿里云 腾讯云 等等 2. 注册域名:阿里云 腾讯云 3. 下载安装PuTTy ...
- Alpha(2/6)
队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 任务分配.进度监督 提交记录(全组共用) 接下来的计划 沟通前后端成员,监督.提醒他们尽快完成各自的进度 还剩下哪些任务 ...
- HDU 6194 string string string ——(2017沈阳网络赛,后缀数组)
思路见:http://blog.csdn.net/aozil_yang/article/details/77929216. 代码如下: #include <stdio.h> #includ ...
- 使用Android的日志工具Log
Android中的日志工具类是Log,这个类中提供了5个方法来供我们打印日志 1.Log.v()用于打印那些最为琐碎的,意义最小的日志信息.对应级别verbose,是Android日志里面级别最低的一 ...
- [WEB安全]绕过URL跳转限制的思路
0x00 简介 说起URL跳转漏洞,有些人可能会觉得,不就是单纯的跳转到某一个其他网页吗?有什么用??? 给大家一个链接,你们进去看一下就明白了: http://www.anquan.us/searc ...
- 数据量与半监督与监督学习 Data amount and semi-supervised and supervised learning
机器学习工程师最熟悉的设置之一是访问大量数据,但需要适度的资源来注释它.处于困境的每个人最终都会经历逻辑步骤,当他们拥有有限的监督数据时会问自己该做什么,但很多未标记的数据,以及文献似乎都有一个现成的 ...
- SpringMVC将通过ajax发送的 json数据封装成JavaBean
SpringMVC将通过ajax发送的 json数据封装成JavaBean 通过ajax发送的 json数据封装成JavaBean对发送时有如下要求: 1.发送的数据类型必须时UTF-8 2.发送的必 ...