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

FB店面面经

🔗
leonardcohen 2016-9-9 12:25:52 | 只看该作者
全局:
zhuhai_ZFC 发表于 2016-8-20 04:33
第一题的follow up我觉得可以用哈希表记录pattern里每个字符的出现次数,然后在string里维持一个pattern大 ...

直接用window, 然后 sorted(window) == sorted(pattern)
提起和面试官商量下能否用 built-in sorted.
回复

使用道具 举报

🔗
leonardcohen 2016-9-9 12:26:49 | 只看该作者
全局:
zyoppy008 发表于 2016-8-20 04:52
都是原题,人品不错啦。。我昨天才面的,都是见到没见过的,连面经都没有的题,还是傻逼烙印给我面的,也挂 ...

烙印 是不是  故意 难为 华人?
回复

使用道具 举报

全局:
第一题的follow up就是minimum window substring? window size=pattern size
回复

使用道具 举报

🔗
 楼主| zhuhai_ZFC 2016-9-19 05:32:44 | 只看该作者
全局:
hezhifeng850207 发表于 2016-9-18 23:04
第一题的follow up就是minimum window substring? window size=pattern size

不是的。要找一个字串,里面的所有字符正好是pattern的一个排列。min window substring是找一个最短字串,包含所有pattern的字符,但同时也可以包含其他非pattern的字符。这里的followup仍然要求字符数量上严格匹配,只是顺序随意。有O(n+k)的方法,不过我的代码比较长。
回复

使用道具 举报

全局:
zhuhai_ZFC 发表于 2016-9-19 05:32
不是的。要找一个字串,里面的所有字符正好是pattern的一个排列。min window substring是找一个最短字串 ...

恩,我的意思是,window size=pattern size的时候,不就是保护所有pattern的字符,并且不包含其他非pattern的字符了么?如果string中存在这么一个window的话,那么这个window size刚好就是pattern size。所以我们找到第一个满足条件的window的时候,直接返回就可以了
回复

使用道具 举报

🔗
 楼主| zhuhai_ZFC 2016-9-20 02:08:40 | 只看该作者
全局:
hezhifeng850207 发表于 2016-9-19 06:17
恩,我的意思是,window size=pattern size的时候,不就是保护所有pattern的字符,并且不包含其他非patte ...

哦 对对对。
回复

使用道具 举报

🔗
 楼主| zhuhai_ZFC 2016-9-20 02:14:07 | 只看该作者
全局:
hezhifeng850207 发表于 2016-9-19 06:17
恩,我的意思是,window size=pattern size的时候,不就是保护所有pattern的字符,并且不包含其他非patte ...

能否贴个代码交流一下?我的代码如下:

  1. public class Solution {
  2.     public int strstrp(String s, String p) {
  3.         if(s==null || p==null || s.length()<p.length()) {
  4.             return -1;
  5.         } else {
  6.             int first = -1;
  7.             int cntUnique = 0;
  8.             int[] cnts = new int[ 256 ];

  9.             Arrays.fill(cnts, Integer.MAX_VALUE);
  10.             for(int idx = 0; idx < p.length(); idx++) {
  11.                 if(cnts[ p.charAt(idx) ] == Integer.MAX_VALUE) {
  12.                     cntUnique++;
  13.                     cnts[ p.charAt(idx) ] = 1;
  14.                 } else {
  15.                     cnts[ p.charAt(idx) ]++;
  16.                 }
  17.             }

  18.             for(int idx = 0; idx < p.length(); idx++) {
  19.                 if(cnts[ s.charAt(idx) ] < Integer.MAX_VALUE) {
  20.                     cnts[ s.charAt(idx) ]--;
  21.                     if(cnts[ s.charAt(idx) ] == 0) {
  22.                         cntUnique--;
  23.                     } else if(cnts[ s.charAt(idx) ] == -1) {
  24.                         cntUnique++;
  25.                     }
  26.                 }
  27.             }
  28.             if(cntUnique == 0) {
  29.                 return 0;
  30.             }

  31.             for(int idx = p.length(); idx < s.length(); idx++) {
  32.                 if(cnts[ s.charAt(idx-p.length()) ] < Integer.MAX_VALUE) {
  33.                     cnts[ s.charAt(idx-p.length()) ]++;
  34.                     if(cnts[ s.charAt(idx-p.length()) ] == 0) {
  35.                         cntUnique--;
  36.                     } else if(cnts[ s.charAt(idx-p.length()) ] == 1) {
  37.                         cntUnique++;
  38.                     }
  39.                 }
  40.                 if(cnts[ s.charAt(idx) ] < Integer.MAX_VALUE) {
  41.                     cnts[ s.charAt(idx) ]--;
  42.                     if(cnts[ s.charAt(idx) ] == 0) {
  43.                         cntUnique--;
  44.                     } else if(cnts[ s.charAt(idx) ] == -1) {
  45.                         cntUnique++;
  46.                     }
  47.                 }

  48.                 if(cntUnique == 0) {
  49.                     return idx-p.length()+1;
  50.                 }
  51.             }

  52.             return first;
  53.         }
  54.     }
  55. }
复制代码
回复

使用道具 举报

全局:
zhuhai_ZFC 发表于 2016-9-20 02:14
能否贴个代码交流一下?我的代码如下:

没有写test case,不知道对不对,求大神莫喷
t是要找的pattern
public class Solution {
    public int minWindow(String s, String t) {
        int[] count=new int[256];
        for (int i=0;i<t.length();i++) count[t.charAt(i)]++;
        int k=0;
        int start=0, end=0;
        while (end<s.length()){
            //k+1 when we find a character in s which is also in t
            if (count[s.charAt(end)]>0) k++;
            count[s.charAt(end)]--;
            while (k==t.length()){
                //return directly when the size of window equals the size of pattern
                if (end-start+1==t.length()) return start;
                if (count[s.charAt(start)]==0) k--;
                count[s.charAt(start)]++;
                start++;
            }
            end++;
        }
        if (minlen==Integer.MAX_VALUE) -1;
        return start;
    }
}
回复

使用道具 举报

🔗
moonyellow 2017-7-9 06:12:45 | 只看该作者
全局:
第二题BFS, 不过电面这么搞也太难了吧。。。
回复

使用道具 举报

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

本版积分规则

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