zoukankan      html  css  js  c++  java
  • [leetcode-884-Uncommon Words from Two Sentences]

    We are given two sentences A and B.  (A sentence is a string of space separated words.  Each word consists only of lowercase letters.)

    A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.

    Return a list of all uncommon words. 

    You may return the list in any order.

    Example 1:

    Input: A = "this apple is sweet", B = "this apple is sour"
    Output: ["sweet","sour"]
    

    Example 2:

    Input: A = "apple apple", B = "banana"
    Output: ["banana"]
    

    Note:

    1. 0 <= A.length <= 200
    2. 0 <= B.length <= 200
    3. A and B both contain only spaces and lowercase letters.

    思路:

    直接利用stringstream 分割处单词来用map统计一下即可。

    vector<string> uncommonFromSentences(string A, string B)
    {
        vector<string>wordA;
        stringstream ss(A + " " + B);
        string t;
        while(ss>>t){wordA.push_back(t);}
        map<string,int>mp;
        for(auto t : wordA){mp[t]++;}    
        vector<string>ret;
        for(auto it = mp.begin(); it != mp.end(); it++)
        {
            if(it->second ==1)ret.push_back(it->first);
        }
        return ret;  
    }
  • 相关阅读:
    Design + Code (iOS)
    Objective-C Programming (2nd Edition)
    iOS Programming The Big Nerd Ranch Guide (4th Edition)
    反射
    面向对象
    人狗大战
    数据结构初识(三级菜单)
    面向对象(组合)
    练习
    re模块
  • 原文地址:https://www.cnblogs.com/hellowooorld/p/9752146.html
Copyright © 2011-2022 走看看