📣 独立日限时特惠: VIP通行证立减$68
楼主: happysshao
跳转到指定楼层
上一主题 下一主题
收起左侧

Google 电面

🔗
averillzheng 2014-11-3 14:08:48 | 只看该作者
全局:
贴个我写的code, 望指点
  1. import java.util.Map;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.ArrayList;

  5. class TrieNode{
  6.         boolean isWord;
  7.         String prefix;
  8.         Map<String, TrieNode> child;
  9.         TrieNode(String prefix, boolean isWord){
  10.                 this.prefix  = prefix;
  11.                 this.isWord = isWord;
  12.         }
  13. }

  14. public class MiniCompression {
  15.         public static List<String> minCompress(String[] input) {               
  16.                 if(input == null)                return null;       
  17.                 List<String> comp = new ArrayList<String>();       
  18.                
  19.                 TrieNode root = buildTrieTree(input);       
  20.                 for(String s : input){
  21.                         TrieNode prex = root, curr        = root;
  22.                         int len = s.length();
  23.                         boolean single = true;
  24.                         int i = 0;
  25.                         for(; i < len; ++i) {                                       
  26.                                 Map<String, TrieNode> mp = curr.child;
  27.                                 if((mp.size() > 1) || (i < len && curr.isWord) )                single = false;
  28.                                 else{
  29.                                         if(!single){
  30.                                                 prex = curr;
  31.                                                 single = true;
  32.                                         }
  33.                                 }                               
  34.                                 curr = mp.get(s.substring(0, i + 1));
  35.                         }
  36.                         if(i == len && !single)                prex = curr;  
  37.                         comp.add(s+":"+prex.prefix);
  38.                 }
  39.                 return comp;
  40.         }
  41.        
  42.         public static TrieNode buildTrieTree(String[] input) {
  43.                 if(input == null)                return null;
  44.                 TrieNode root = new TrieNode("", false);
  45.                 if(input.length > 0) {
  46.                         for(String s : input){
  47.                                 int len = s.length();
  48.                                 TrieNode curr = root;
  49.                                 for(int i = 1; i < len + 1; ++i) {
  50.                                         if(curr.child == null)                curr.child = new HashMap<String, TrieNode>();                                       
  51.                                         if(curr.child.containsKey(s.substring(0, i))) {
  52.                                                 if(i == len)         curr.child.get(s.substring(0, i)).isWord = true;                                               
  53.                                         }else{
  54.                                                 curr.child.put(s.substring(0, i), new TrieNode(s.substring(0, i), i == len));
  55.                                         }
  56.                                         curr = curr.child.get(s.substring(0, i));
  57.                                 }
  58.                         }
  59.                 }
  60.                 return root;
  61.         }
  62.        
  63.         public static void main(String[] args) {
  64.                 String[] input = new String[]{"zebra", "dog", "duck", "dot"};
  65.                 System.out.println(minCompress(input));
  66.         }
  67. }
复制代码
回复

使用道具 举报

🔗
averillzheng 2014-11-3 14:12:13 | 只看该作者
全局:
Arthur2012 发表于 2014-10-27 05:13
请问:ab,abb,abbb这种情况怎么办,第一个word是后面word的一个前缀

这种情况下,{ab:“”, abb: "abb", abbb:"abbb"}
回复

使用道具 举报

🔗
Arthur2012 2014-11-3 15:46:08 | 只看该作者
全局:
averillzheng 发表于 2014-11-3 14:12
这种情况下,{ab:“”, abb: "abb", abbb:"abbb"}

既然ab是"",为什么abb不也是呀?
abb也还是abbb的前缀呀
回复

使用道具 举报

🔗
王可雪 2014-11-3 17:26:29 | 只看该作者
全局:
第一种output挺简单,5-10分钟就能写完。后面改的那种要加count才行。
  1. #include <string>
  2. #include <unordered_map>
  3. #include <vector>
  4. #include <iostream>
  5. using std::string;
  6. using std::unordered_map;
  7. using std::cout;
  8. using std::vector;
  9. using std::endl;

  10. struct TrieNode{
  11.         char val;
  12.         int count;
  13.         //bool used;
  14.         unordered_map<char, TrieNode*> children;
  15.         TrieNode(char x) : val(x), count(0) {};       
  16. };

  17. class Trie {
  18. private:
  19.         TrieNode *root;
  20. public:
  21.         Trie() {
  22.                 root = new TrieNode(0);
  23.                 //root->count = -1;
  24.         }

  25.         ~Trie() {
  26.                 Trie_deconst_util(root);
  27.         }

  28.         void Trie_deconst_util(TrieNode *root) {
  29.                 if (!root)
  30.                         return;

  31.                 for (auto &x : root->children) {
  32.                         Trie_deconst_util(x.second);
  33.                 }

  34.                 delete root;
  35.         }

  36.         void insertTrie(string word) {
  37.                 //string substr;
  38.                 TrieNode *node = root;
  39.                 //bool IdontWantToUseFlag = false;
  40.                 for (auto x : word) {
  41.                         node->count++;
  42.                         if (node->children.find(x) == node->children.end()) {
  43.                                 node->children[x] = new TrieNode(x);
  44.                                 node = node->children[x];
  45.                                 continue;
  46.                                 //if (!IdontWantToUseFlag)
  47.                                 //substr += x;
  48.                                 //IdontWantToUseFlag = true;
  49.                                 //node = node->children[x];
  50.                                 //continue;
  51.                                 //return substr;
  52.                         }
  53.                         node = node->children[x];
  54.                         //if (!IdontWantToUseFlag)
  55.                         //substr += x;
  56.                 }
  57.         }

  58.         string searchTrie(string word) {
  59.                 string substr;
  60.                 TrieNode *node = root;
  61.                 for (auto x : word) {
  62.                         substr += x;
  63.                         node = node->children[x];
  64.                         if (node->count <= 1) {
  65.                                 //substr += x;
  66.                                 return substr;
  67.                         }
  68.                 }
  69.                 return "";
  70.         }
  71. };

  72. unordered_map<string, string> shortest_prefix(vector<string> &words) {
  73.         unordered_map<string, string> ans;
  74.         Trie dict;
  75.         for (auto word : words) {
  76.                 dict.insertTrie(word);
  77.         }
  78.         for (auto word : words) {
  79.                 ans[word] = dict.searchTrie(word);
  80.         }
  81.         return ans;
  82. }

  83. int main() {
  84.         vector<string> input({"zebra", "dog", "duck","dot"});
  85.         unordered_map<string, string> ans = shortest_prefix(input);
  86.         for (auto x : ans) {
  87.                 cout << x.first << " | " << x.second << endl;
  88.         }
  89. }
复制代码
回复

使用道具 举报

🔗
averillzheng 2014-11-3 23:12:00 | 只看该作者
全局:
王可雪 发表于 2014-11-3 17:26
第一种output挺简单,5-10分钟就能写完。后面改的那种要加count才行。

真牛,5-10分钟能写这么多。好要思考
回复

使用道具 举报

🔗
王可雪 2014-11-4 01:43:16 | 只看该作者
全局:
averillzheng 发表于 2014-11-3 23:12
真牛,5-10分钟能写这么多。好要思考

最开始那种output建个trie的过程中就得到结果了,也没什么corner case所以比较好写。以前用lisp写过trie,再加上前两天参加个比赛刚写了个c++的trie,所以比较熟。不过悲剧的发现前两天写的那个貌似有memory leak。
回复

使用道具 举报

🔗
flyaway25 2014-11-4 07:37:26 | 只看该作者
全局:
zhenggao1986 发表于 2014-11-3 13:13
class PrefixTreeNode {
        public int count;
        public PrefixTreeNode[] next;

你的code里面有个小bug,不能test这种情况"dog" and "dogg",应该前者返回空字符后者返回"dogg"。改正的话我觉得需要把get那个function最后返回的不应该是word,应该是new String();,只要是找到count是1的情况下就可以返回前缀,如果不出现count是1的情况就应该直接返回空。上例中的dog就不会出现count是1的时候,而dogg则在最后一位出现1。
回复

使用道具 举报

🔗
zhenggao1986 2014-11-4 10:58:26 | 只看该作者
全局:
flyaway25 发表于 2014-11-4 07:37
你的code里面有个小bug,不能test这种情况"dog" and "dogg",应该前者返回空字符后者返回"dogg"。改正的 ...

这个bug好修,关键是对要求的理解,我觉得输出 dog dogg挺好的,输出个空有点怪异

回复

使用道具 举报

🔗
guomin1314 2014-11-4 15:05:06 | 只看该作者
全局:
24垅的代码不对,比如 [log]
回复

使用道具 举报

🔗
sunnyroom 2014-11-4 15:16:45 | 只看该作者
全局:
哎,狗狗确实难啊。电面都这么难
回复

使用道具 举报

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

本版积分规则

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