Leetcode 299. Bulls and Cows
2016-05-29 22:06
369 查看
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.
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.
class Solution { public: // only contains digits string getHint(string secret, string guess) { int aCnt = 0; int bCnt = 0; vector<int> sVec(10, 0); // 0 ~ 9 for secret vector<int> gVec(10, 0); // 0 ~ 9 for guess if (secret.size() != guess.size() || secret.empty()) { return "0A0B"; } for (int i = 0; i < secret.size(); ++i) { char c1 = secret[i]; char c2 = guess[i]; if (c1 == c2) { ++aCnt; } else { ++sVec[c1-'0']; ++gVec[c2-'0']; } } // count b for (int i = 0; i < sVec.size(); ++i) { bCnt += min(sVec[i], gVec[i]); } return to_string(aCnt) + 'A' + to_string(bCnt) + 'B'; } };
相关文章推荐
- EMIPLIB库分析三 MIPWAVInput
- iOS程序开发引用的第三方库之间出现duplicate symbol时的处理方法
- unity avatar 初探
- 余额宝(1)
- 学习 shell —— 参数的解析:$0/1/2、$#、 $@、$?(set -e)
- EMIPLIB库分析二
- 团队项目第二次冲刺Ⅴ
- linux 进程管理
- HDU5715 XOR游戏
- 小白进阶之冒泡排序、归并排序、选择排序、冒泡排序
- 第13周项目2 (3)
- EMIPLIB库分析一
- autoLayout自动布局约束技巧
- CodeForces 300 A Array
- c++ 对象内存布局详解
- 例题5-4 UVa156 Ananagrams(STL:map)
- 在Ubuntu 12.04安装和设置SSH服务
- 23种设计模式3--结构型模式(适配器模式、装饰模式、代理模式、外观模式、桥接模式、组合模式、享元模式)
- 自定义CircleProgressView控件
- 第二阶段冲刺第五天