📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
12
返回列表 发新帖
楼主: 进击的菜鸟
跳转到指定楼层
上一主题 下一主题
收起左侧

发一个Google面经

🔗
woridage1 2016-4-15 20:44:11 | 只看该作者
全局:
做一个函数取后两个字母,然后遍历所有单词,相同后缀中最长的记录一下长度,取出这个后缀,用折半查找从序列里找到位置,然后前后比较,比较出最大数字,取出来相加。遍历加折半  nlogn,不知道这样做可以不可以,请指教~
回复

使用道具 举报

🔗
woridage1 2016-4-15 20:52:49 | 只看该作者
全局:
或者做一个map<string, pair<int 前缀最大,int 后缀最大>> 遍历一遍,每个单词取前缀存到map对应string的pair->first,取后缀存到对应pair->second。当然每次都存的是max值,这个就涉及到一个问题,如果一个单词前缀和后缀相同的话,能不能算一对,不能的话就得把这个特殊情况给处理一下。
回复

使用道具 举报

全局:
  1.         public int longestPair(List<String> input) {
  2.                 HashMap<String, String> hm = new HashMap<>();
  3.                 int res = 0;
  4.                 //suppose all input are valid. size >= 2
  5.                 for (String s : input) {
  6.                         String _key = s.substring(s.length() - 1) + s.substring(0, 2);
  7.                         System.out.println(_key);
  8.                         if (hm.containsKey(_key)) {
  9.                                 res = Math.max(s.length() + hm.get(_key).length(), res);
  10.                         }
  11.                         String key = s.substring(0, 1) + s.substring(s.length() - 2);
  12.                         String value = hm.get(key);
  13.                         if (value == null || value.length() < s.length()) {
  14.                                 hm.put(key, s);
  15.                         }
  16.                 }
  17.                 return res;
  18.         }
复制代码
回复

使用道具 举报

🔗
houqingniao 2016-4-26 03:35:39 | 只看该作者
全局:
最长的 apple+lemma吧?
回复

使用道具 举报

🔗
Littles 2016-4-29 07:27:49 | 只看该作者
全局:
  1.         private static int max = 0;
  2.         public static int findPair(String[] words) {

  3.                 if (words == null || words.length == 0) {
  4.                         return 0;
  5.                 }
  6.                 for (int i = 0; i < words.length; i++) {
  7.                         if (words[i].length() < 2) {
  8.                                 continue;
  9.                         }
  10.                         binarySearch(words[i].substring(words[i].length()-2), words, words[i].charAt(0), 0, words.length-1, i);
  11.                 }
  12.                 return max;
  13.         }
  14.        
  15.         public static void binarySearch(String lastTwo, String[] words, char c, int lo, int hi, int i) {
  16.                 if (lo > hi) {
  17.                         return;
  18.                 }
  19.                 while (lo <= hi) {
  20.                         int mid = lo + (hi-lo)/2;
  21.                         if (words[mid].length() < 2) {
  22.                                 binarySearch(lastTwo, words, c, lo, mid-1, i);
  23.                                 binarySearch(lastTwo, words, c, mid+1, hi, i);
  24.                         } else if (words[mid].startsWith(lastTwo)) {
  25.                                 if (words[mid].charAt(words[mid].length()-1) == c) {
  26.                                         max = Math.max(words[i].length()+words[mid].length(), max);
  27.                                 }
  28.                                 binarySearch(lastTwo, words, c, lo, mid-1, i);
  29.                                 binarySearch(lastTwo, words, c, mid+1, hi, i);
  30.                                 return;
  31.                         } else if (words[mid].substring(0,2).compareTo(lastTwo) < 0) {
  32.                                 lo = mid+1;
  33.                         } else {
  34.                                 hi = mid-1;
  35.                         }
  36.                 }       
  37.         }
复制代码
回复

使用道具 举报

🔗
wangmengcathy 2016-4-30 01:12:24 | 只看该作者
全局:

一次遍历需要俩map吧
回复

使用道具 举报

🔗
jjustc 2016-6-17 05:33:47 | 只看该作者
全局:
这题如果 要省时间 那可以用俩map 如果 要省空间可以用 binary search

问一下 最后面试官说could we do better 是指的什么呀?…… 想不出哪方面可以还优化了呢。。。。谢谢!
回复

使用道具 举报

🔗
何打发123 2016-10-23 03:05:47 | 只看该作者
全局:
感谢分享! 请问楼主不论是建立map还是二分找 最坏情况复杂度都可能是n^2对吗?~ 就是所有单词的前两位都和后两位相同?~
回复

使用道具 举报

🔗
何打发123 2016-10-23 03:12:40 | 只看该作者
全局:

这个map的value应该是arraylist 因为可能有多个string都是这样的开头加结尾0.0
回复

使用道具 举报

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

本版积分规则

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