努橙刷题编

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

https://leetcode.com/problems/find-duplicate-file-in-system/

public class Solution {
    public List<List<String>> findDuplicate(String[] paths) {
        List<List<String>> result = new ArrayList<>();
        Map<String, List<String>> map = new HashMap<>();
        for (String s : paths) {
            String[] tmp = s.split("[ ]");
            for (int i = 1; i < tmp.length; i++) {
                String[] file = tmp[i].split("[ \\(\\)]");
                if (!map.containsKey(file[1])) {
                    map.put(file[1], new ArrayList<>());
                }
                map.get(file[1]).add(tmp[0] + "/" + file[0]);
            }
        }
        for (Map.Entry<String, List<String>> entry: map.entrySet()) {
            if (entry.getValue().size() > 1) {
                result.add(new ArrayList<>(entry.getValue()));
            }
        }
        return result;
    }
}

 

posted on 2017-06-06 14:35  努橙  阅读(249)  评论(0)    收藏  举报