Bulls and Cows
2015-12-16 13:27
176 查看
Total Accepted: 11158 Total Submissions: 42823 Difficulty: Easy
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:
Hint:
Write a function to return a hint according to the secret number and friend's guess, use
Please note that both secret number and friend's guess may contain duplicate digits, for example:
In this case, the 1st
You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.
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:
1bull and
3cows. (The bull is
8, the cows are
0,
1and
7.)
Write a function to return a hint according to the secret number and friend's guess, use
Ato indicate the bulls and
Bto 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
1in friend's guess is a bull, the 2nd or 3rd
1is 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.
class Solution { public: string getHint(string secret, string guess) { int secret_map[256] ; memset(secret_map,0,sizeof(secret_map)); for(auto c:secret){ secret_map[c]++; } int acnt = 0,bcnt =0; int gsize = guess.size(); int ssize = secret.size(); for(int i=0;i<gsize && i<ssize;i++){ if(guess[i] == secret[i]){ acnt++; secret_map[guess[i]]--; } } for(int i=0;i<gsize && i<ssize;i++){ if(guess[i] != secret[i] && secret_map[guess[i]]!=0){ bcnt++; secret_map[guess[i]]--; } } string res; ostringstream oss(res); oss<<acnt<<"A"<<bcnt<<"B"; return oss.str(); } };
相关文章推荐
- java 深入技术六(Map)
- 所谓软件工程
- 我眼中的“helloworld”——缓冲区
- win7安装appserv后无法登陆phpmyadmin的解决办法,太牛了,找了我半天
- iOS 单例模式(Singleton)
- 谈程序的正确性
- facebook强大的图片加载工具Fresco,比imageload加载速度快
- Swift 的基本数据类型 常量和变量、String 的基本操作
- iOS开发~interface Builder(简称 IB) 界面构建器
- S_ISDIR 、S_ISLNK等几个常见的宏
- OpenWRT开发之——BuildPackage剖析
- 【mybatis】基本配置和使用
- 对每个网站自定义页面脚本展示 - Chome 扩展
- php(2)
- OC学习之property讲解
- NDK 与 JNI 的关系
- Android-JNI(4)-简单函数的实现
- java唯一无序id生成
- js函数Math random()、ceil()、floor()、round() 比较
- Android之ListView分页获取网路数据客户端实现(开启异步任务,获取网络数据)(三)