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.
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.
链接: http://leetcode.com/problems/bulls-and-cows/
题解:
公牛和奶牛游戏。使用HashMap存下来secret里的字符和count,然后同时遍历secret和guess就可以了。最后还要遍历一次map把多加的cow减掉。
Time Complexity - O(n), Space Complexity - O(n)
public class Solution {
public String getHint(String secret, String guess) {
if(secret == null || guess == null || secret.length() != guess.length()) {
return "0A0B";
}
int bulls = 0, cows = 0;
Map<Character, Integer> map = new HashMap<>(); for(int i = 0; i < secret.length(); i++) {
char c = secret.charAt(i);
if(!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
} for(int i = 0; i < secret.length(); i++) {
char sChar = secret.charAt(i);
char gChar = guess.charAt(i);
if(sChar == gChar) {
bulls++;
map.put(gChar, map.get(gChar) - 1);
} else if(map.containsKey(gChar)) {
cows++;
map.put(gChar, map.get(gChar) - 1);
}
} for(char c : map.keySet()) {
if(map.get(c) < 0) {
cows += map.get(c);
}
} return String.valueOf(bulls) + "A" + String.valueOf(cows) + "B";
}
}
二刷:
主要参考了Discuss里面的解。
- 我们可以用一个数组来存bulls和cows。用s和g来表示数组的数字值
- 当 s = g时,我们找到了bull, bulls++
- 否则我们要看
- nums[g] > 0的话,说明当前guess的这个数字曾经出现在secret中,这是一个cow,我们cow++
- 我们也要看是否nums[s] < 0, 这个表明当前ssecret的数字曾经出现在guess中,这也是一个cow,我们还是cow++
- 我们用正数记录下nums[s]为bull的一个位置,nums[s]++, 我们也用负数记录下guess中出现过的数字,nums[g]--,
Java:
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public String getHint(String secret, String guess) {
if(secret == null || guess == null || secret.length() != guess.length()) {
return "0A0B";
}
int[] nums = new int[10];
for (int i = 0; i < secret.length(); i++) {
int s = secret.charAt(i) - '0';
int g = guess.charAt(i) - '0';
if (s == g) {
bulls++;
} else {
if (nums[s] < 0) { // bulls can be counted as cows
cows++;
}
if (nums[g] > 0) { // found num but in diff position
cows++;
}
nums[s]++;
nums[g]--;
}
}
return bulls + "A" + cows + "B";
}
}
三刷:
延续了二刷的解法。主要使用一个count[]数组保存之前出现过的secret digits和guess digits。当前sDigit == gDigit时,bulls增加。 否则, 当count[sDigit] < 0时,说明之前出现在guess里, 当count[gDigit] > 0时,说明之前出现在secret里,这两种情况都要分别增加cows。之后再记录i这个位置的改动count[sDigit]++, count[gDigit]--。最后返回结果。
Java:
public class Solution {
public String getHint(String secret, String guess) {
if (secret == null || guess == null || secret.length() != guess.length()) {
return "0A0B";
}
int bulls = 0;
int cows = 0;
int[] count = new int[10];
for (int i = 0; i < secret.length(); i++) {
int sDigit = secret.charAt(i) - '0';
int gDigit = guess.charAt(i) - '0';
if (sDigit == gDigit) {
bulls++;
} else {
if (count[sDigit] < 0) {
cows++;
}
if (count[gDigit] > 0) {
cows++;
}
}
count[sDigit]++;
count[gDigit]--;
}
return bulls + "A" + cows + "B";
}
}
Update:
public class Solution {
public String getHint(String secret, String guess) {
if (secret == null || guess == null || secret.length() != guess.length()) return "0A0B";
int bullsCount = 0, cowsCount = 0;
int len = secret.length();
int[] count = new int[10]; for (int i = 0; i < len; i++) {
int sc = secret.charAt(i) - '0';
int gc = guess.charAt(i) - '0';
if (sc == gc) {
bullsCount++;
} else {
if (count[gc] > 0) cowsCount++;
if (count[sc] < 0) cowsCount++;
count[sc]++;
count[gc]--;
}
}
return bullsCount + "A" + cowsCount + "B";
}
}
Reference:
https://leetcode.com/discuss/67031/one-pass-java-solution
299. Bulls and Cows的更多相关文章
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 299. Bulls and Cows - LeetCode
Question 299. Bulls and Cows Solution 题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一 ...
- 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- [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 猜数字游戏
你正在和你的朋友玩猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为”Bulls“, 公牛),有多少位数字 ...
- [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 ...
- 【leetcode❤python】 299. Bulls and Cows
#-*- coding: UTF-8 -*-class Solution(object): def getHint(self, secret, guess): " ...
随机推荐
- java 顺序表
想看看java版的数据结构,了解一下树的一些操作,写了个顺序表熟悉一下 package com.sqlist; /** * @author xiangfei * 定义一个顺序表 * */ public ...
- 解释型语言和编译型语言如何交互?以lua和c为例
转自http://my.oschina.net/mayqlzu/blog/113528 问题: 最近lua很火,因为<愤怒的小鸟>使用了lua,ios上有lua解释器?它是怎么嵌入大ios ...
- 关于java.lang.OutOfMemoryError: Java heap space的错误分析
今天无意间遇到这个错误:java.lang.OutOfMemoryError: Java heap space 问题出现原因:使用a标签实现快速下载[当然已经实现了,但想了想还是要归纳解决这类问题] ...
- 数据类型的处理(提取自FMDB)
if ((!obj) || ((NSNull *)obj == [NSNull null])) { sqlite3_bind_null(pStmt, idx); } // FIXME - someda ...
- CSS中的视觉格式化模型
视觉格式化模型 1. 简介 在视觉格式化模型中,文档树中的每个元素都将会根据盒模型产生零到多个盒子.这些盒子的布局由如下因素决定: 盒子的尺寸和类型 定位策略(正常文档流,浮动或者绝对定位) 和文档树 ...
- JS中函数的基础知识
函数 一. 函数定义 函数又叫方法,在程序里面函数是用来执行某些特定功能的代码.为了减少重复使用代码,可以把特定功能的代码做成函数,需要使用时拿出来调用.alert();就是一个很常见的.简单的函数 ...
- 查看Linux下*.a库文件中文件、函数、变量等情况
在Linux 下经常需要链接一些 *.a的库文件,那怎么查看这些*.a 中包 含哪些文件.函数.变量: 1. 查看文件:ar -t *.a 2. 查看函数.变里:nm *.a
- Hadoop伪分布模式配置
本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 请先按照上一篇文章H ...
- 如何在Asp.net中备份Access数据库?
public void Create( string mdbPath ) { if( File.Exists(mdbPath) ) //检查数据库是否已存在 { thr ...
- 集合类 Collection
1.Collection接口有两个子接口: List:保存元素顺序的线性表,允许有重复元素. Set:不记录元素的保存顺序,不允许有重复元素.数学中的集合 Collection接口中的方法如下: Co ...