299.LeetCode Bulls and Cows(easy)[字符串处理 map]
来源:互联网 发布:ios 可变字典添加数据 编辑:程序博客网 时间:2024/05/29 18:33
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.
题目中bulls的个数是位置和值都相同的元素个数,cows是位置不同但是元素值相同的元素个数。这个题的解题思路是首先用两个map分别存储字符和位置的集合,然后对guess的map循环处理,找到具有相同char的位置集合,对两个集合比较(这个比较过程因为位置的大小是顺序的,所以可以很好控制指针的前进步伐,有点类似与归并排序的Merge过程),记录位置相同的个数加到bulls上,最后比较位置不相同的但是元素相同的个数,取两个map中最小的作为cows的部分之一。最后汇总得到所有的bulls和cows.
</pre><pre code_snippet_id="1647853" snippet_file_name="blog_20160414_1_1382983" name="code" class="cpp">class Solution {public: string getHint(string secret, string guess) { //解题思路:用两个map存储对应相同char的位置信息,然后对两个串的相同的char查找是否有相同的位置 int bull = 0,cows = 0; map<char,vector<int> > se; map<char,vector<int> > ge; if(secret.size() != guess.size() ) return "0A0B"; for(int i= 0;i<secret.size();i++) { se[secret[i]].push_back(i); ge[guess[i]].push_back(i); } map<char,vector<int> >::iterator iter = ge.begin(); for(;iter != ge.end();iter++) { char c = iter->first; vector<int> p = iter->second; if(se.count(c) != 0) { map<char,vector<int> >::iterator piter = se.find(c); vector<int> q = piter->second; int n1 = q.size(); int n2 = p.size(); int i = 0,j=0; int temp = 0; while(i<p.size()&&j<q.size()) { if(p[i] == q[j]) { ++temp; ++i;++j; }else if(p[i]<q[j]) { ++i; }else { ++j; } } cows += n1-temp<n2-temp?n1-temp:n2-temp; bull += temp; } } stringstream ss,ss1; ss<<bull; ss1<<cows; string r = ss.str()+'A'+ ss1.str() +'B'; return r; }};
- 299.LeetCode Bulls and Cows(easy)[字符串处理 map]
- Leetcode 299. Bulls and Cows (Easy) (cpp)
- leetcode--- Bulls and Cows---字符串
- <LeetCode><Easy>299 Bulls and Cows
- [leetcode] 299. Bulls and Cows
- LeetCode || 299. Bulls and Cows
- LeetCode 299. Bulls and Cows
- Leetcode 299. Bulls and Cows
- LeetCode.299.Bulls and Cows
- LeetCode--299. Bulls and Cows
- 【LeetCode】299. Bulls and Cows
- [leetcode] 299. Bulls and Cows
- LeetCode *** 299. Bulls and Cows
- leetcode 299. Bulls and Cows
- LeetCode 299. Bulls and Cows
- LeetCode-299.Bulls and Cows
- Leetcode 299. Bulls and Cows
- 299.[leetcode]Bulls and Cows
- strcpy、memcpy、strncpy函数实现
- 编程之美 - 浮点数的精确表示
- assets文件夹下的系列图片获取存list
- java中文乱码解决之道(三)-----编码详情:伟大的创想---Unicode编码
- UIWebView的使用方法
- 299.LeetCode Bulls and Cows(easy)[字符串处理 map]
- p587有限自动机
- VS2015 无法解析的外部符号 __vsnwprintf_s
- 网络_HttpURLConnection_原始类
- [C语言][LeetCode][141]Linked List Cycle
- java中文乱码解决之道(四)-----java编码转换过程
- Java基础--并发编程基础(1)
- [SpringBoot实践]spring-data-mongo自定义Repository接口及其实现
- android 开发 时间的计算问题