Find shortest snippet

来源:互联网 发布:淘宝怎么拉黑店铺 编辑:程序博客网 时间:2024/06/06 10:40

参考:点击打开链接

Find shortest snippet,比如给一个Document是A,X,X,B,A,X,B,Query是A,B,要求返回shortestSnippet

第一问:如果Query有序(即A一定要在B前面),那么要返回A,X,B
Follow up:如果Query无序(即B在A前面也可以),那么要返回B,A

再Follow up:如果Document非常大,如何再优化?

public static void main(String[] args) {String[] document = "A B C D E C B C A L I N".split(" ");        ShortestSnippet sn = new ShortestSnippet(document);        System.out.println(sn.query("A", "C"));        System.out.println(sn.query2("A", "C"));}private HashMap<String, ArrayList<Integer>> hashmap;private String[] document;public ShortestSnippet(String[] document) {if (document == null || document.length == 0) {throw new IllegalArgumentException();}this.document = document;hashmap = new HashMap<String, ArrayList<Integer>>();for (int i = 0; i < document.length; i++) {String word = document[i];if (!hashmap.containsKey(word)) {hashmap.put(word, new ArrayList<Integer>());}hashmap.get(word).add(i);}}public String query(String wordA, String wordB) {if (wordA == null || wordB == null) {throw new IllegalArgumentException();}if (!hashmap.containsKey(wordA) || !hashmap.containsKey(wordB)) {return "not found";}ArrayList<Integer> listA = hashmap.get(wordA);ArrayList<Integer> listB = hashmap.get(wordB);int i = 0, j = 0, minA = 0, minB = 0, minDist = Integer.MAX_VALUE;//一定是要保证wordA在wordB的前面while (i < listA.size() && j < listB.size()) {if (listA.get(i) > listB.get(j)) {//wordA在wordB的后面,让wordB的指针后移j++;} else {//找到最小的距离if (minDist > listB.get(j) - listA.get(i)) {minDist = listB.get(j) - listA.get(i);minA = listA.get(i);minB = listB.get(j);}i++;}}StringBuilder sb = new StringBuilder();//输出最小距离中的所有单词for (int k = minA; k <= minB; k++) {sb.append(document[k]);}return sb.toString();}// follow uppublic String query2(String wordA, String wordB) {if (wordA == null || wordB == null) {throw new IllegalArgumentException();}if (!hashmap.containsKey(wordA) || !hashmap.containsKey(wordB)) {return "not found";}ArrayList<Integer> listA = hashmap.get(wordA);ArrayList<Integer> listB = hashmap.get(wordB);int i = 0, j = 0, minA = 0, minB = 0, minDist = Integer.MAX_VALUE;while (i < listA.size() && j < listB.size()) {int posA = listA.get(i), posB = listB.get(j);if (minDist > Math.abs(posA - posB)) {minDist = Math.abs(posA - posB);minA = posA;minB = posB;}if (posA < posB) {i++;} else {j++;}}StringBuilder sb = new StringBuilder();int start = Math.min(minA, minB), end = Math.max(minA, minB);for (int k = start; k <= end; k++) {sb.append(document[k]);}return sb.toString();}


0 0