输入: [ { “id”: 1, “content”: “This is the content of document 1 it is very short” }, { “id”: 2, “content”: “This is the content of document 2 it is very long bilabial bilabial heheh hahaha …” }, ] 输出: { “This”: [1, 2], “is”: [1, 2], … } 例 2:
/** * Definition of Document: * class Document { * public: * int id; * string content; * } */ class Solution { public: /** * @param docs a list of documents * @return an inverted index */ map<string, vector<int>> invertedIndex(vector<Document>& docs) { // Write your code here stringstream ss; map<string, vector<int>> m; for (int i = 0; i < docs.size(); i++) { int m_id = docs[i].id; ss << docs[i].content; for (string str; ss >> str; m[str].push_back(m_id)); ss.clear(); } for (map<string, vector<int>>::iterator it = m.begin(); it != m.end(); it++) { it->second.erase(unique(it->second.begin(), it->second.end()), it->second.end()); } return m; } };
-------------end of file
thanks for reading-------------