299. Bulls and Cows - LeetCode
Question

Solution
题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一个cow,注:数字对与位置对优先,一个号码不能重复判断.
思路:构造map结构,遍历实现
Java实现:实现的不漂亮,好歹能通过
public String getHint(String secret, String guess) {
Map<Character, Index> map = new HashMap<>();
for (int i=0; i<secret.length(); i++) {
Index idx = map.get(secret.charAt(i));
if (idx == null) {
idx = new Index();
map.put(secret.charAt(i), idx);
}
idx.add(i);
}
int bulls = 0;
int cows = 0;
List<Character> cowsList = new ArrayList<>(); // for count cows
// count bulls
for (int i=0; i<guess.length(); i++) {
Index idx = map.get(guess.charAt(i));
if (idx != null) { // check digits
if (idx.isBull(i)) {
bulls++;
} else {
cowsList.add(guess.charAt(i));
}
}
}
// count cows
for (char c : cowsList) {
Index idx = map.get(c);
if (idx.isCow()) {
cows++;
}
}
return bulls + "A" + cows + "B";
}
class Index {
List<Integer> idxList;
int count;
// constructor
public Index() {
idxList = new ArrayList<>();
count = 0;
}
void add(int x) {
idxList.add(x);
count++;
}
boolean isCow() {
return count-- > 0;
}
boolean isBull(int x) {
for (int tmp : idxList) {
if (x == tmp) {
count--;
return true;
}
}
return false;
}
}
Ref
https://leetcode.com/problems/bulls-and-cows/discuss/74621/One-pass-Java-solution
public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] numbers = new int[10];
for (int i = 0; i<secret.length(); i++) {
int s = Character.getNumericValue(secret.charAt(i));
int g = Character.getNumericValue(guess.charAt(i));
if (s == g) bulls++;
else {
if (numbers[s] < 0) cows++;
if (numbers[g] > 0) cows++;
numbers[s] ++;
numbers[g] --;
}
}
return bulls + "A" + cows + "B";
}
public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] numbers = new int[10];
for (int i = 0; i<secret.length(); i++) {
if (secret.charAt(i) == guess.charAt(i)) bulls++;
else {
if (numbers[secret.charAt(i)-'0']++ < 0) cows++;
if (numbers[guess.charAt(i)-'0']-- > 0) cows++;
}
}
return bulls + "A" + cows + "B";
}
299. Bulls and Cows - LeetCode的更多相关文章
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【一天一道LeetCode】#299. Bulls and Cows
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- LeetCode 299 Bulls and Cows
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- [leetcode]299. 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 ...
- 299 Bulls and Cows 猜数字游戏
你正在和你的朋友玩猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为”Bulls“, 公牛),有多少位数字 ...
- Bulls and Cows leetcode
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- [LC] 299. Bulls and Cows
Example 1: Input: secret = "1807", guess = "7810" Output: "1A3B" Expla ...
- Leetcode 299 Bulls and Cows 字符串处理 统计
A就是统计猜对的同位同字符的个数 B就是统计统计猜对的不同位同字符的个数 非常简单的题 class Solution { public: string getHint(string secret, s ...
随机推荐
- 攻防世界 NaNNaNNaNNaN-Batman
NaNNaNNaNNaN-Batman 下载出一个文件我们一开始不知道是个啥,我们拉入到sublime中看一下 我们可以发现在最开始的位置有一个_是一段函数变量,最后的eva()那个是执行函数代码,但 ...
- CPU架构:CPU架构详细介绍
1 概述 CPU架构是CPU商给CPU产品定的一个规范,主要目的是为了区分不同类型的CPU.目前市场上的CPU分类主要分有两大阵营,一个是intel.AMD为首的复杂指令集CPU,另一个是以IBM.A ...
- 什么是pandas
- 电源PCB布板的10个基本法则
电容模型 电容并联高频特性 电感模型 电感特性 镜象面概念 高频交流电流环路 过孔 (VIA) 的例子 PCB板层分割 降压式(BUCK)电源:功率部分电流和电压波形 降压式电源排版差的例子 电路等效 ...
- 走在 SVG + Low Poly 的路上
随着 SVG 的发展,艺术家和设计师们把越来越多传统设计行业的东西引入了前端, low poly 就是其中之一.那 low poly 强大在哪呢,大家通过下面的图来感受一下. 恰巧我们产品 Logo ...
- python-图的字典表示
图的字典表示.输入多行字符串,每行表示一个顶点和该顶点相连的边及长度,输出顶点数,边数,边的总长度.比如上图0点表示:{'O':{'A':2,'B':5,'C':4}}.用eval函数处理输入,eva ...
- hive启动出错
Hive启动报错:java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument - 狗子的进阶史 - ...
- 世界各国 MCC 和 MNC 列表
http://www.cnblogs.com/inteliot/archive/2012/08/22/2651666.html常见MCC:代码(MCC) ISO 3166-1 国家202 ...
- Android设置TextView为不可见
通常控件的可见与不可见分为三种情况. 第一种 gone 表示不可见并且不占用空间 第二种 visible 表示可见 第三种 invisible 表示 ...
- 火狐浏览器Hackbar安装破解
1 下载 https://pan.baidu.com/s/18cKoJAam9by7AB168Im57g 64mt 下载后解压到一个固定文件夹下 2 安装 选择xpi进行安装 3 关闭插件更新 点击插 ...