Inverted Index
来源:互联网 发布:js将图片上传到服务器 编辑:程序博客网 时间:2024/05/28 23:12
Create an inverted index with given documents.
这是一道字符串相关的题目,实际上并不难,其中的一个主要的问题是正则表达式的使用,对于java来说可以直接使用string中的方法split(“\\s+”)作为切分方式,在python需要导入re模块
java
/** * Definition of Document: * class Document { * public int id; * public String content; * } */public class Solution { /** * @param docs a list of documents * @return an inverted index */ public Map<String, List<Integer>> invertedIndex(List<Document> docs) { // Write your code here Map<String, List<Integer>> map = new HashMap<>(); int id; String str; String[] arr; for (int i = 0; i < docs.size(); i++) { id = docs.get(i).id; str = docs.get(i).content; arr = str.trim().split("\\s+"); for (int j = 0; j < arr.length; j++) { util(map, arr[j], id); } } return map; } private void util(Map<String, List<Integer>> map, String str, int id) { if (!map.containsKey(str)) { List<Integer> list = new ArrayList<>(); list.add(id); map.put(str, list); } else { List<Integer> list = map.get(str); if (list.get(list.size() - 1) == id) { return; } else { list.add(id); map.put(str, list); } } }}
python
'''Definition of Documentclass Document: def __init__(self, id, cotent): self.id = id self.content = content'''import reclass Solution: # @param {Document[]} docs a list of documents # @return {dict(string, int[])} an inverted index def invertedIndex(self, docs): # Write your code here mapping = {} for i in range(len(docs)): idd = docs[i].id content = docs[i].content.strip() arr = re.split('\\s+', content) for ele in arr: self.util(mapping, ele, idd) return mapping def util(self, mapping, string, idd): if string not in mapping: mapping[string] = [idd] else: path = mapping[string] if path[-1] == idd: return else: path.append(idd) mapping[string] = path
阅读全文
0 0
- Inverted Index
- Inverted Index
- Inverted Index in practice
- The Inverted Multi-Index
- Python implement inverted index
- MapReduce Inverted index
- LintCode Inverted Index
- Inverted Multi-Index
- 反向索引(Inverted Index)
- 倒排索引(Inverted index)
- 倒排索引(Inverted Index)
- 反向索引(Inverted Index)
- 在信息检索中应用Inverted Index
- 倒排索引(Inverted File Index )
- Pinot中的Inverted Index源码分析
- Inverted Index(倒排索引)
- 搜索引擎技术之反向索引设计(inverted index):
- Lucene Inverted index(倒排索引)原来快速入门
- LeetCode 61. Rotate List
- unable to add window -- token null is not valid; is your activity running 错误解决办法
- 配置Linux系统tomcat开机自启动
- 五子棋-java版本
- 顺序表查找、有序表查找、索引顺序表查找
- Inverted Index
- 使用localResizeIMG插件上传图片到七牛云返回链接通过PHP进行保存
- XSS测试代码大全
- 十月十日学习心得
- LeetCode 83. Remove Duplicates from Sorted List
- fullpage.js——回调函数
- 真正的离开都是悄然无息的
- unity3d中获得物体的size
- VMware硬件兼容性查询链接