LintCode Inverted Index

原题网址:http://www.lintcode.com/en/problem/inverted-index/

Create an inverted index with given documents.

 Notice

Ensure that data does not include punctuation.

Example

Given a list of documents with id and content. (class Document)

[
  {
    "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 ..."
  },
]

Return an inverted index (HashMap with key is the word and value is a list of document ids).

{
   "This": [1, 2],
   "is": [1, 2],
   ...
}
方法:使用哈希影射。

/**
 * 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, Set<Integer>> map = new HashMap<>();
        for(Document doc : docs) {
            String[] strs = doc.content.split(" ");
            for(String str : strs) {
                if ("".equals(str)) continue;
                update(str, doc.id, map);
            }
        }
        Map<String, List<Integer>> results = new HashMap<>();
        for(Map.Entry<String, Set<Integer>> entry : map.entrySet()) {
            List<Integer> list = new ArrayList<>(entry.getValue());
            Collections.sort(list);
            results.put(entry.getKey(), list);
        }
        return results;
    }
    private void update(String str, int id, Map<String, Set<Integer>> results) {
        Set<Integer> set = results.get(str);
        if (set == null) {
            set = new HashSet<>();
            results.put(str, set);
        }
        set.add(id);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值