mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
add 299
This commit is contained in:
parent
d56d9a7462
commit
d4df5c09ab
@ -223,6 +223,7 @@ My LeetCode solutions with Chinese explanation. 我的LeetCode中文题解。
|
||||
| 292 |[Nim Game](https://leetcode.com/problems/nim-game)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/292.%20Nim%20Game.md)|Easy| |
|
||||
| 295 |[Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/)|[C++](solutions/295.%20Find%20Median%20from%20Data%20Stream.md)|Hard| |
|
||||
| 297 |[Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)|[C++](solutions/297.%20Serialize%20and%20Deserialize%20Binary%20Tree.md)|Hard| |
|
||||
| 299 |[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/)|[C++](solutions/299.%20Bulls%20and%20Cows.md)|Medium| |
|
||||
| 300 |[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/300.%20Longest%20Increasing%20Subsequence.md)|Medium| |
|
||||
| 301 |[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)|[C++](solutions/301.%20Remove%20Invalid%20Parentheses.md)|Hard| |
|
||||
| 303 |[Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/303.%20Range%20Sum%20Query%20-%20Immutable.md)|Easy| |
|
||||
|
55
solutions/299. Bulls and Cows.md
Normal file
55
solutions/299. Bulls and Cows.md
Normal file
@ -0,0 +1,55 @@
|
||||
# [299. Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/)
|
||||
|
||||
# 思路
|
||||
A的个数比较简单,只需要挨个位置比较即可。B的个数,需要先用一个数组记录secret中各数字出现的个数(需排除A情况),再遍历一遍看guess中的数字是否在secret中出现过,若是,则B个数加一。
|
||||
|
||||
|
||||
# C++
|
||||
|
||||
|
||||
```C++
|
||||
class Solution {
|
||||
public:
|
||||
string getHint(string secret, string guess) {
|
||||
int a = 0, b = 0;
|
||||
vector<int>mp(10, 0);
|
||||
for(int i = 0; i < secret.size(); i++){
|
||||
if(secret[i] != guess[i]) mp[secret[i] - '0']++;
|
||||
}
|
||||
for(int i = 0; i < secret.size(); i++){
|
||||
if(secret[i] == guess[i]) a++;
|
||||
else if(mp[guess[i] - '0'] > 0){
|
||||
b++;
|
||||
mp[guess[i] - '0']--;
|
||||
}
|
||||
}
|
||||
|
||||
return to_string(a) + "A" + to_string(b) + "B";
|
||||
}
|
||||
};
|
||||
```
|
||||
也可以只遍历一遍:
|
||||
```C++
|
||||
class Solution {
|
||||
public:
|
||||
string getHint(string secret, string guess) {
|
||||
int a = 0, b = 0;
|
||||
vector<int>mp1(10, 0);
|
||||
vector<int>mp2(10, 0);
|
||||
for(int i = 0; i < secret.size(); i++){
|
||||
if(secret[i] != guess[i]){
|
||||
mp1[secret[i] - '0']++;
|
||||
mp2[guess[i] - '0']++;
|
||||
}else{
|
||||
a++;
|
||||
}
|
||||
|
||||
}
|
||||
for(int i = 0; i < 10; i++){
|
||||
b += min(mp1[i], mp2[i]);
|
||||
}
|
||||
|
||||
return to_string(a) + "A" + to_string(b) + "B";
|
||||
}
|
||||
};
|
||||
```
|
Loading…
Reference in New Issue
Block a user