楼主: kunzi
跳转到指定楼层
上一主题 下一主题
收起左侧

脸书家onsite

🔗
fubu 2016-11-6 13:36:44 | 只看该作者
全局:
贴我的代码,我这儿'*'是match 0个或多个前缀字符的 '*' Matches zero or more of the preceding element

  1. public class WordDictionary {
  2.     TrieNode root;
  3.     WordDictionary() {
  4.         root = new TrieNode();
  5.     }
  6.     // Adds a word into the data structure.
  7.     public void addWord(String word) {
  8.         TrieNode cur = root;
  9.         for(int i=0; i<word.length(); i++) {
  10.             char c = word.charAt(i);
  11.             int index = c - 'a';
  12.             if(cur.children[index] == null) {
  13.                 cur.children[index] = new TrieNode();
  14.             }
  15.             cur = cur.children[index];
  16.             cur.letter = c;
  17.         }
  18.         cur.hasWord = true;
  19.     }

  20.     public boolean search(String word) {
  21.         TrieNode cur = root;
  22.         return search(root, word, 0);
  23.     }
  24.    
  25.     private boolean search(TrieNode cur, String word, int index) {
  26.         if(cur == null) {
  27.             return false;
  28.         }
  29.         if(index == word.length()) {
  30.             return cur.hasWord;
  31.         }
  32.         char c = word.charAt(index);
  33.         if(index == word.length() - 1 || word.charAt(index+1) != '*') {
  34.             if(c == '.') {
  35.                 for(TrieNode node : cur.children) {
  36.                     if(search(node, word, index+1)) {
  37.                         return true;
  38.                     }
  39.                 }
  40.                 return false;
  41.             } else if(c == '*') {
  42.                 return false;
  43.             } else {
  44.                 return search(cur.children[c - 'a'], word, index+1);
  45.             }
  46.         } else {
  47.             if(search(cur, word, index+2)) {
  48.                 return true;
  49.             }
  50.             if(c == '.') {
  51.                 for(TrieNode node : cur.children) {
  52.                     if(search(node, word, index)) {
  53.                         return true;
  54.                     }
  55.                 }
  56.                 return false;
  57.             } else {
  58.                 return search(cur.children[c - 'a'], word, index);
  59.             }
  60.         }

  61.     }
  62.    
  63.     class TrieNode {
  64.         TrieNode[] children;
  65.         char letter;
  66.         boolean hasWord;
  67.         TrieNode() {
  68.             children = new TrieNode[26];
  69.             hasWord = false;
  70.         }
  71.     }
  72. }
复制代码













回复

使用道具 举报

🔗
zzgzzm 2016-11-6 13:44:03 | 只看该作者
全局:
liurudahai 发表于 2016-9-29 00:44
不对,应该是这样,分两种情况,因为他可以匹配0个字符,所以直接可以用*后面那个字符在当前层搜索,如果 ...

这个应该是对当前层所有的descendants递归吧,不光是children. 因为*可匹配任意长度。例如字典只有"abcd", 搜索“a*d". 在匹配a之后,其所有子孙树:“bcd", "cd", "d"都要和*之后的“d"比较才不会误漏字典的词。当然也许我误解你具体如何实现的,若有请见谅。
回复

使用道具 举报

🔗
kittyhk2009 2016-11-6 14:45:04 | 只看该作者
本楼:
全局:
多谢楼主!
回复

使用道具 举报

🔗
knight0clk 2016-11-6 23:11:49 | 只看该作者
全局:
zzgzzm 发表于 2016-11-6 10:50
这个应该是FB的经典高频题了,地里有很多面经。就是先让自己设计一个class Vec表示欧式空间的N维向量v =  ...

binary search vs two pointers,binary search怎么用来解决dot product?可以细说一下吗?之前我就是感觉two pointers和hashmap。
回复

使用道具 举报

🔗
zzgzzm 2016-11-7 00:06:02 | 只看该作者
全局:
knight0clk 发表于 2016-11-6 23:11
binary search vs two pointers,binary search怎么用来解决dot product?可以细说一下吗?之前我就是感 ...

这实际上就是在于怎么求2个sorted arrays 的所有相交元素。对每个长array 的元素在短array 中binary search. 注意每次若未找到match 的话一定要记录当前最接近的index. 不要每次都从头binary search.
回复

使用道具 举报

🔗
knight0clk 2016-11-7 00:31:01 | 只看该作者
全局:
zzgzzm 发表于 2016-11-7 00:06
这实际上就是在于怎么求2个sorted arrays 的所有相交元素。对每个长array 的元素在短array 中binary sear ...

大哥你厉害,我懂了,多谢!最坏的复杂度nlogm,貌似也可以接受。
回复

使用道具 举报

🔗
hello2pig 2016-11-21 03:34:36 | 只看该作者
全局:
zzgzzm 发表于 2016-11-6 10:50
这个应该是FB的经典高频题了,地里有很多面经。就是先让自己设计一个class Vec表示欧式空间的N维向量v =  ...

Got it! thanks!  
回复

使用道具 举报

🔗
bigbearlake 2017-3-20 07:46:25 | 只看该作者
全局:
zzgzzm 发表于 2016-11-6 12:56
这个应该就是LC211的扩展了,加上了support for '*'. 关键是当search word遇到'*',要跳过所有可能连续的 ...

你这个假设是'*'可以替代任何character. 但是如果是替代previous character就不行了
回复

使用道具 举报

🔗
bigbearlake 2017-3-20 07:54:55 | 只看该作者
全局:
fubu 发表于 2016-11-6 13:36
贴我的代码,我这儿'*'是match 0个或多个前缀字符的 '*' Matches zero or more of the preceding element[/ ...

if(search(cur, word, index+2)) {
                return true;
            }
怎么直接返回true了? 'abc'也可以进入这个,但是不应该返回true吧
回复

使用道具 举报

🔗
lhh_NJU 2017-3-20 09:31:55 | 只看该作者
全局:
liurudahai 发表于 2016-9-29 00:44
不对,应该是这样,分两种情况,因为他可以匹配0个字符,所以直接可以用*后面那个字符在当前层搜索,如果 ...

我觉得只有当*前面是一个'.'的情况才需要对所有children遍历吧, 如果是一个普通字符, 就遍历那个字符对应的children就好.  
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表