leetcode 299 Bulls and Cows C++
2016-06-05 16:47
471 查看
也是参考了别人的做法。
两个大小为10的数组。
相同位置相同则直接将bull+1;
否则把数字存入数组。
然后把每个位置较小的数累加到cow里面即可。
string getHint(string secret, string guess) {
int bullNum = 0;
int cowNum = 0;
vector<int> bullArray(10,0);
vector<int> cowArray(10,0);
for(int i = 0;i<secret.size();i++){
if(secret[i]==guess[i]){
bullNum++;
} else {
bullArray[secret[i] - '0']++;
cowArray[guess[i] - '0']++;
}
}
for(int i = 0;i<10;i++){
cowNum += min(bullArray[i],cowArray[i]);
}
return to_string(bullNum) + 'A' + to_string(cowNum) + 'B';
}
两个大小为10的数组。
相同位置相同则直接将bull+1;
否则把数字存入数组。
然后把每个位置较小的数累加到cow里面即可。
string getHint(string secret, string guess) {
int bullNum = 0;
int cowNum = 0;
vector<int> bullArray(10,0);
vector<int> cowArray(10,0);
for(int i = 0;i<secret.size();i++){
if(secret[i]==guess[i]){
bullNum++;
} else {
bullArray[secret[i] - '0']++;
cowArray[guess[i] - '0']++;
}
}
for(int i = 0;i<10;i++){
cowNum += min(bullArray[i],cowArray[i]);
}
return to_string(bullNum) + 'A' + to_string(cowNum) + 'B';
}
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解