[leetcode]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: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.
Example 2:
Input: secret = "1123", guess = "0111" Output: "1A1B" Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow.
题意:
猜字游戏。
我负责猜,
你负责给提示:数字和位置都对,bulls++。数字对位置不对,cows++。

思路:
挨个扫字符串s,
挨个扫字符串g
若s当前字符等于g的字符,bulls++
用int[] map = new int[256]建一个简化版的HashMap
思路类似valid anagram
s当前字符在map里标记为++
p当前字符在map里标记为--
那么,若s当前字符已经被标记为--,说明p的字符来标记过,即它们字符相同但位置不同,cow++。
同理,若p当前字符已经被标记为++, 说明s的字符来标记过, 即它们字符相同但位置不同,cow++。
代码:
class Solution {
public String getHint(String secret, String guess) {
// corner
if(secret.length() != guess.length()) return false;
int[] map = new int[256];
int bull = 0;
int cow = 0;
for(int i = 0; i < secret.length();i++){
char s = secret.charAt(i);
char g = guess.charAt(i);
if(s == g){
bull++;
}else{
if(map[s]<0) cow++;
if(map[g]>0) cow++;
map[s]++;
map[g]--;
}
}
return bull +"A" + cow + "B";
}
}
[leetcode]299. Bulls and Cows公牛和母牛的更多相关文章
- LeetCode OJ:Bulls and Cows (公牛与母牛)
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- 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 字符串处理 统计
A就是统计猜对的同位同字符的个数 B就是统计统计猜对的不同位同字符的个数 非常简单的题 class Solution { public: string getHint(string secret, s ...
- 【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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- 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“, 公牛),有多少位数字 ...
- Java [Leetcode 229]Bulls and Cows
题目描述: You are playing the following Bulls and Cows game with your friend: You write down a number an ...
随机推荐
- C#后台调用前台javascript的五种方法小结
第一种,OnClientClick (vs2003不支持这个方法) <asp:Button ID="Button1" runat="server" Tex ...
- 在ie6下的png图片的兼容问题
png图片在ie6下是这样的: 正确样式: 这样解决: html代码: <body> <div class="gys"></div> </ ...
- mono部分源码解析
一.源码结构 这里仅列举几个重要的目录:mcs: mcs: Mono实现的基于Ecma标准的C#编译器. class: CLI的C#级的实现.类似于Android中的Java层,应用程序看 ...
- unity3d中物体的控制
一.物体的循环移动和旋转 思路:通过对时间的计算,每隔一段时间让物体旋转,实现来回移动. float TranslateSpeed = 0.02f; float TranslateSpeedTime ...
- JavaScript中判断函数、变量是否存在
转载:http://www.jb51.net/article/67551.htm 一.是否存在指定函数 function isExitsFunction(funcName) { try { if (t ...
- 马尔可夫毯(Markov Blanket)
马尔可夫毯(Markov Blanket) 最近接触到马尔可夫毯(MarkovBlanket)这个概念,发现网上资料不多,通俗易懂的解释甚少,查了一些资料后,决定写一个总结. 提到马尔可夫毯,就会有一 ...
- Python函数名为参数
1.定义两个函数,求和函数和最大函数 def add(x, y): return x + y def maxnum(x, y): return x if x > y else y lst= [2 ...
- 树结构之JavaScript
对于数据结构“树”,想必大家都熟悉,今儿,我们就再来回顾一下数据结构中的二叉树与树,并用JavaScript实现它们. ps:树结构在前端中,很多地方体现得淋漓尽致,如Vue的虚拟DOM以及冒泡等等. ...
- leetcode130
struct POS { int x; int y; POS(int newx, int newy): x(newx), y(newy) {} }; class Solution { public: ...
- Jsch - java SFTP 文件上传下载
使用Jsch上传.下载文件,核心步骤是:获取channel,然后使用get/put方法下载.上传文件 核心代码句: session = jSch.getSession(ftpUserName, ftp ...