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 ...
随机推荐
- 语法速学,返回数组 repeated修饰符
重新编写proto文件 syntax = "proto3"; package services; import "google/api/annotations.proto ...
- PHP流程控制之分支结构switch语句的使用
分支结构switch语句的使用 还记得我们最开始讲了这么一个故事: 王同学家里头特别有钱,所以他的行程方式和正常人的又有些不一样. 他的出行方式呢有6种,如下: 1,司机开车2,民航3,自己家的专机4 ...
- YAML_05 定义一个变量创建用户,设置密码
ansible]# vim user2.yml --- - hosts: cache remote_user: root vars: user: wangwu tasks: ...
- redis堵死致数据清空
情景: zy的链路监控突然都恢复,而且在哪个时间段zabbix中显示回复,也发送了告警,但是实际上告警并没有发出来.这是不可能的情况,应该是redis缓存中的数据都被清空了,没有认为干预,需解决问题 ...
- WAMP 403 Forbidden禁止访问,别的电脑访问不了;
直接上图: 1:修改httpd.conf; deny from all 改成------ allow from all 重启服务就好了: 2:如果搜不到deny from all 就按照下面的方法来 ...
- lixuxmint系统定制与配置(5)-效率配置
小书匠Linux 目录: 1.zsh安装与配置 1.1 安装 1.1.1 检查当前的终端类型 1.1.2 安装zsh 1.2 美化zsh 1.3 配置zsh 1.3.1 别名设置 2.自动登录服务器 ...
- 【概率论】5-7:Gama分布(The Gamma Distributions Part I)
title: [概率论]5-7:Gama分布(The Gamma Distributions Part I) categories: - Mathematic - Probability keywor ...
- OpenStack Restful API框架介绍
1 pecan框架介绍 1.1 什么是pecan pecan是一个轻量级的python web框架,最主要的特点是提供了简单的配置即可创建一个wsgi对象并提供了基于对象的路由方式. 主要提供的功 ...
- GoCN每日新闻(2019-09-27)
1. Golang新版本发布:Go 1.13.1和Go 1.12.10https://golang.org/dl/ 2. 如何在Golang中使用Websockets:最佳工具和步骤指南 https: ...
- 如何修复GitKraken Inotify Limit Error\idea erro - 升级Ubuntu / Linux inotify限制
GitKraken是一个非常优秀的Git客户端.如果您是软件开发人员,那么您绝对应该试试GitKraken.今天我去了我的一个存储库做了一些提交,但是GitKraken告诉我它已经得到了Inotify ...