class Solution {
public:
    void SplitString(const string& s, vector<string>& v, const string& c)
    {
        string::size_type pos1, pos2;
        pos2 = s.find(c);
        pos1 = 0;
        while (string::npos != pos2)
        {
            v.push_back(s.substr(pos1, pos2 - pos1));

            pos1 = pos2 + c.size();
            pos2 = s.find(c, pos1);
        }
        if (pos1 != s.length())
            v.push_back(s.substr(pos1));
    }
    vector<string> uncommonFromSentences(string A, string B) {
        vector<string> X;
        map<string, int> M;

        vector<string> V1;        
        SplitString(A, V1, " ");
        for (auto str : V1)
        {
            if (M.find(str) == M.end())//未找到此值
            {
                M.insert(make_pair(str, 1));
            }
            else
            {
                M[str]++;
            }
        }

        vector<string> V2;
        SplitString(B, V2, " ");
        for (auto str : V2)
        {
            if (M.find(str) == M.end())//未找到此值
            {
                M.insert(make_pair(str, 1));
            }
            else
            {
                M[str]++;
            }
        }

        for (auto m : M)
        {
            if (m.second == 1)
            {
                X.push_back(m.first);
            }
        }
        return X;
    }
};

 

posted on 2018-09-28 07:36  Sempron2800+  阅读(108)  评论(0编辑  收藏  举报