This commit is contained in:
ShusenTang 2022-10-26 22:48:25 +08:00
parent d56d9a7462
commit d4df5c09ab
2 changed files with 56 additions and 0 deletions

View File

@ -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| | | 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| | | 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| | | 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| | | 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| | | 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| | | 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| |

View 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";
}
};
```