leetcode884

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

猜你喜欢

转载自www.cnblogs.com/asenyang/p/9716612.html
今日推荐