SP1 某云ES倒排索引
描述
某云ES的文件管理系统中,文档由一个int型的ID和字符串类型的文档描述content组成,请根据提供的文档ID和文档描述,设计对搜索单词的倒排索引,根据输入要查找的单词,输出对应的文档ID。
示例1
输入:
[1, 5, 4, 9],["My lover", "Yours", "you are young", "My old age"],"My"
复制返回值:
[1,9]
复制
备注:
单词区分大小写
题解
思路:
很简单,暴力遍历、查找即可~~
代码如下:
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param ID int整型vector
* @param content string字符串vector
* @param word string字符串
* @return int整型vector
*/
vector<int> invertedIndex(vector<int> &ID, vector<string> &content, string word)
{
vector<int> ans;
for (int i = 0; i < content.size(); ++i)
{
if (content[i].find(word) != std::string::npos)
{
ans.push_back(ID[i]);
}
}
return ans;
}
};