12
返回列表 发新帖
楼主: david123bbs
跳转到指定楼层
上一主题 下一主题
收起左侧

Google电面, 求人品

🔗
bigbearlake 2017-1-18 02:04:33 | 只看该作者
全局:

可以说一下大概思路吗?
回复

使用道具 举报

🔗
laiguojiuhao 2017-1-18 02:50:28 | 只看该作者
全局:
bigbearlake 发表于 2017-1-18 02:04
可以说一下大概思路吗?

就是建个trie暴力搜的;如果这个词的当前字母不匹配,就检查‘*’在不在:不在返回错,否则用'*'依次替代1~len(rest of word)长度的字母继续搜
回复

使用道具 举报

🔗
期末求过 2017-1-18 21:22:02 | 只看该作者
全局:
可不可以每次考虑用*和不用*, 用*的话,26个试一边,不用*的话,直接跳到下个比较,?
回复

使用道具 举报

🔗
littleMiao 2017-1-23 08:02:08 | 只看该作者
全局:
bbmbill 发表于 2017-1-13 09:14
这道题能不能把每个trie node的子节点加一个‘*’的情况,指向node自己,然后dfs?这样follow up的问题应该 ...

你好,好像你这样解决不了比如 fo*d, fodddd这样的case
回复

使用道具 举报

🔗
spiritrhy 2017-2-7 06:45:47 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
shameka0425 2017-2-10 15:05:35 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
陈sisyphus 2018-4-26 06:49:21 | 只看该作者
全局:
写个java版本的吧, 楼主上面的例子应该都能过:
  1. public class FilteredTrie {
  2.     static class TrieNode{
  3.         char ch;
  4.         boolean hasStar= false;
  5.         boolean isEnd= false;
  6.         TrieNode[] children= new TrieNode[26];
  7.         public TrieNode(char ch){
  8.             this.ch= ch;
  9.         }

  10.         @Override
  11.         public String toString(){
  12.             return this.ch+" hasStar:"+ hasStar+ "  isEnd:"+ isEnd;
  13.         }
  14.     }
  15.     static class Trie{
  16.         TrieNode root= new TrieNode('*');
  17.         public Trie(String[] words){
  18.             for(String word: words){
  19.                 TrieNode node= root;
  20.                 for(int i=0; i<word.length(); i++){
  21.                     char ch= word.charAt(i);
  22.                     if(ch=='*'){
  23.                         node.hasStar= true;
  24.                         continue;
  25.                     }
  26.                     if(node.children[ch-'a']==null){
  27.                         node.children[ch-'a']= new TrieNode(ch);
  28.                     }
  29.                     node= node.children[ch-'a'];
  30.                 }
  31.                 node.isEnd= true;
  32.             }
  33.         }
  34.         public boolean search(String word){
  35.             return search(word, root);
  36.         }
  37.         public boolean search(String word, TrieNode _node){
  38.             if(word.length()==0) return _node.isEnd;
  39.             TrieNode node= _node;
  40.             for(int i=0; i<word.length(); i++){
  41.                 char ch= word.charAt(i);
  42.                 if(node.hasStar){
  43.                     for(int len=0; i+len<=word.length(); i++){
  44.                         node.hasStar= false;
  45.                         if(search(word.substring(i+len), node)){
  46.                             node.hasStar= true;
  47.                             return true;
  48.                         }
  49.                         node.hasStar= true;
  50.                     }
  51.                     return false;
  52.                 }else{
  53.                     if(node.children[ch-'a']==null)  return false;
  54.                     node= node.children[ch-'a'];
  55.                 }
  56.             }
  57.             return node.isEnd;
  58.         }
  59.     }
  60.     public static void main(String[] args){
  61.         String[] filters= new String[]{"h*o", "fo*d", "fo*de"};
  62.         Trie trie= new Trie(filters);
  63.         String[] words= new String[]{"food", "hello", "foo", "fod", "foood", "focfd", "fodede"};
  64.         for(String word: words){
  65.             System.out.println(trie.search(word));
  66.         }
  67.     }
  68. }
复制代码
回复

使用道具 举报

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

本版积分规则

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