GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/1268. Search Suggestions System at master · feiteng/leetcode Expand file tree/ Copy path1268. Search Suggestions SystemMore file actions 48 lines (41 loc) · 1.45 KB / Copy path1268. Search Suggestions SystemFile metadata and controls48 lines (41 loc) · 1.45 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 class Solution { class Trie { Trie[] child = new Trie[26]; Queue<String> q = new PriorityQueue<>(); void insert(String s) { Trie root = this; for(char c : s.toCharArray()) { if(root.child[c - 'a'] == null) root.child[c - 'a'] = new Trie(); root.q.add(s); root = root.child[c - 'a']; } root.q.add(s); } List<String> search(String s) { Trie root = this; for(char c : s.toCharArray()) { if(root.child[c - 'a'] == null) return new ArrayList<>(); root = root.child[c - 'a']; } // System.out.println(s); // System.out.println(root.q); Queue<String> q2 = new PriorityQueue<>(root.q); List<String> list = new ArrayList<>(); while(!q2.isEmpty() && list.size() < 3) list.add(q2.poll()); return list; } } Trie trie = new Trie(); public List<List<String>> suggestedProducts(String[] products, String searchWord) { List<List<String>> re = new ArrayList<>(); int n = searchWord.length(); for(String p : products) trie.insert(p); for(int i = 1; i <= n; i++) { String sub = searchWord.substring(0, i); re.add(trie.search(sub)); } return re; } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |