299. Bulls and Cows
2015-12-26 22:48
447 查看
题目:
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:
and
(The bull is
the cows are
Write a function to return a hint according to the secret number and friend's guess, use
indicate the bulls and
return
Please note that both secret number and friend's guess may contain duplicate digits, for example:
In this case, the 1st
friend's guess is a bull, the 2nd or 3rd
a cow, and your function should return
You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.
解题思路:
https://leetcode.com/discuss/69558/c-simple-solution-using-array-with-explaination-4ms?show=69558#q69558
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.
解题思路:
https://leetcode.com/discuss/69558/c-simple-solution-using-array-with-explaination-4ms?show=69558#q69558
class Solution { public: string getHint(string secret, string guess) { int bulls=0, cows=0; int nums[10]; memset(nums,0,sizeof(int)*10); for(int i=0;i<secret.size();i++) { if(secret[i]==guess[i])bulls++; else{ int a = secret[i]-'0', b = guess[i]-'0'; cows += (int)(nums[a]<0) + (int)(nums[b]>0); nums[a]++; nums[b]--; } } return to_string(bulls)+'A'+to_string(cows)+'B'; } };
class Solution(object): def getHint(self, secret, guess): """ :type secret: str :type guess: str :rtype: str """ bulls = sum(map(operator.eq,secret,guess)) s = collections.Counter(secret) g = collections.Counter(guess) cows = sum((s&g).values()) - bulls return str(bulls)+'A'+str(cows)+'B'
相关文章推荐
- 通过ViewDragHelper实现ListView的Item的侧拉划出效果
- Hdu 2099之解题报告
- spring异常
- Google 发布 Android 性能优化典范
- set -e
- hdu大学生程序设计竞赛(2015’12)1006 01 Matrix
- 数据库_mysql_基础
- 波段选股
- 【数据结构+算法】【模拟法】--- 开灯问题
- java中的内部类
- 读取文件操作
- Ubuntu上搭建DokuWiki
- 数据结构学习笔记——算法复杂性分析
- 排序 Codeforces612D The Union of k-Segments
- C#中timer类 在C#里关于定时器类就有3个
- 使用Spring MVC统一异常处理实战
- 快速排序
- JavaScript-----语法
- CSS2.1SPEC:视觉格式化模型之包含块
- c语言变量与常量