LeetCode OJ: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".
经典的小游戏,hashtable解决之,代码如下:
class Solution {
public:
string getHint(string secret, string guess) {
unordered_map<char, int> hash;
int countA, countB;
countA = countB = ;
vector<bool> solved(secret.size(), false);
for(int i = ; i < secret.size(); ++i){
++hash[secret[i]];
}
for(int i = ; i < secret.size(); ++i){
if(secret[i] == guess[i]){
countA++;
solved[i] = true;
--hash[secret[i]];
}
}
for(int i = ; i < guess.size(); ++i){
if(!solved[i] && hash[guess[i]] > ){
countB++;
--hash[guess[i]];
}
}
return toString(countA) + "A" + toString(countB) + "B";
}
string toString(int i)
{
stringstream ss;
ss << i;
string tmp;
ss >> tmp;
return tmp;
}
};
LeetCode OJ:Bulls and Cows (公牛与母牛)的更多相关文章
- [leetcode]299. 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 ...
- Java [Leetcode 229]Bulls and Cows
题目描述: You are playing the following Bulls and Cows game with your friend: You write down a number an ...
- LeetCode(45)-Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- Leetcode 299 Bulls and Cows 字符串处理 统计
A就是统计猜对的同位同字符的个数 B就是统计统计猜对的不同位同字符的个数 非常简单的题 class Solution { public: string getHint(string secret, s ...
- [LeetCode] Bulls and Cows 公母牛游戏
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- [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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
随机推荐
- JMeter5.0 边界提取器使用
需求: 需要提取下图中requestNo的值,使用JMeter3.1和4.0版本,使用正则表达式提取器始终无法获取 而后使用JMeter5.0的边界提取器,不需要写复杂的正则表达式,只要填写左右边界即 ...
- python替换一个文件里面的特定内容
f = open("1.txt", "r", encoding="utf-8") f_new = open("2.txt" ...
- Linux查看系统与内核信息(uname、file和lsb_release -a)
uname 命令 uname 命令可以用来查看系统与内核的相关信息,命令格式如下: [root@localhost ~]# uname [选项] 选项: -a:查看系统所有相关信息: -r:查看内核版 ...
- s3cmd安装
配置yum.repos cd /etc/yum.repos.d/ vim s3tools.repo [s3tools] name=Tools for managing Amazon S3 - Simp ...
- MAC OS X的命令行技巧
##透明度#降低透明度defaults write com.apple.universalaccess reduceTransparency -bool true#恢复默认透明度defaults wr ...
- JVM 内存调优 与 实际案例
堆内存设置 原理 JVM堆内存分为2块:Permanent Space 和 Heap Space. Permanent 即 持久代(Permanent Generation),主要存放的是Java类定 ...
- mac iterm2 打开Linux 服务器文件乱码
我的mac 上用是iterm2终端, Shell 环境是zsh.ssh 到Linux 服务器上查看一些文件时,中文乱码. 这种情况一般是终端和服务器的字符集不匹配,MacOSX下默认的是utf8字符集 ...
- codeforces600E Lomsat gelral
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- javascript闭包和立即执行函数的作用
一.闭包——closure 先看一个闭包的例子.我们想实现一个计数器,最简单的方法就是定义一个全局变量,计数的时候将其加1.但是全局变量有风险,哪里都有可能不小心改掉它.那局部变量呢, 它只在函数内部 ...
- HAproxy的安装配置及动静分离
/////////////////////////////目录//////////////////////////////////////////一.安装HAproxy二.编写HAproxy启动脚本三 ...