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 01 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;    }};


0 0
原创粉丝点击