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

问一道Google面经

全局:

2016(1-3月) 码农类General 硕士 实习@google - Other - 技术电面  | | Other | 应届毕业生

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
求问一道在网上看到的Google家面经:

求string str1中含有string
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
c”

只想到brute force,有没有其它好办法?

上一篇:彭博 电面
下一篇:说下亚麻群面,也算是提前给地里即将要群面同学泼盆冷水

本帖被以下淘专辑推荐:

全局:
muybienw 发表于 2016-1-20 07:11
上面帖子的代码格式有问题... 重新贴一下

用三个指针就行了,时间复杂度O(n^2),空间复杂度O(1)
  1. public class ShortestSequence {

  2.         public static void main(String[] args) {
  3.                 System.out.println(getShortestSequence("acbacbc", "abc"));
  4.         }
  5.         public static int getShortestSequence(String s1, String s2) {
  6.                 if (s1.length() < s2.length()) {
  7.                         return Integer.MAX_VALUE;
  8.                 }
  9.                 int res = Integer.MAX_VALUE;
  10.                 for (int i = 0; i < s1.length(); i++) {
  11.                         if (s1.charAt(i) == s2.charAt(0)) {
  12.                                 int j = i;
  13.                                 int k = 0;
  14.                                 while (j < s1.length() && k < s2.length()) {
  15.                                         if (s1.charAt(j) == s2.charAt(k)) {
  16.                                                 j++;
  17.                                                 k++;
  18.                                         } else {
  19.                                                 j++;
  20.                                         }
  21.                                 }
  22.                                 if (k == s2.length()) {
  23.                                         res = Math.min(res, j - i);
  24.                                 }
  25.                         }
  26.                 }
  27.                
  28.                
  29.                 return res;
  30.         }
  31. }
复制代码
回复

使用道具 举报

推荐
muybienw 2016-1-20 07:09:39 | 只看该作者
全局:
用dp写了一下,欢迎讨论~
  1. <font size="1">    <span style="color: rgb(204, 120, 50);">public </span>String <span style="color: rgb(255, 198, 109);">findShortest</span>(String a<span style="color: rgb(204, 120, 50);">, </span>String b){
  2.         <span style="color: rgb(204, 120, 50);">if</span>(a==<span style="color: rgb(204, 120, 50);">null </span>|| b==<span style="color: rgb(204, 120, 50);">null </span>|| a.length()==<span style="color: rgb(104, 151, 187);">0 </span>|| b.length()==<span style="color: rgb(104, 151, 187);">0</span>) <span style="color: rgb(204, 120, 50);">throw new </span>IllegalArgumentException()<span style="color: rgb(204, 120, 50);">;
  3. </span><span style="color: rgb(204, 120, 50);">
  4. </span><span style="color: rgb(204, 120, 50);">        int </span>lena = a.length()<span style="color: rgb(204, 120, 50);">, </span>lenb = b.length()<span style="color: rgb(204, 120, 50);">;
  5. </span><span style="color: rgb(204, 120, 50);">        int</span>[][] dp = <span style="color: rgb(204, 120, 50);">new int</span>[lenb][lena]<span style="color: rgb(204, 120, 50);">;
  6. </span><span style="color: rgb(204, 120, 50);">
  7. </span><span style="color: rgb(204, 120, 50);">        for</span>(<span style="color: rgb(204, 120, 50);">int </span>i=<span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">; </span>i<lenb<span style="color: rgb(204, 120, 50);">; </span>i++){
  8.             <span style="color: rgb(204, 120, 50);">char </span>bc = b.charAt(i)<span style="color: rgb(204, 120, 50);">;
  9. </span><span style="color: rgb(204, 120, 50);">            for</span>(<span style="color: rgb(204, 120, 50);">int </span>j=<span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">; </span>j<lena<span style="color: rgb(204, 120, 50);">; </span>j++){
  10.                 <span style="color: rgb(204, 120, 50);">char </span>ac = a.charAt(j)<span style="color: rgb(204, 120, 50);">;
  11. </span><span style="color: rgb(204, 120, 50);">                </span>dp[i][j] = Integer.<span style="color: rgb(152, 118, 170); font-style: italic;">MAX_VALUE</span><span style="color: rgb(204, 120, 50);">;
  12. </span><span style="color: rgb(204, 120, 50);">
  13. </span><span style="color: rgb(204, 120, 50);">                if</span>(ac==bc){
  14.                     <span style="color: rgb(204, 120, 50);">if</span>(i==<span style="color: rgb(104, 151, 187);">0</span>) dp[i][j] = <span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;
  15. </span><span style="color: rgb(204, 120, 50);">                    else </span>{
  16.                         <span style="color: rgb(204, 120, 50);">for </span>(<span style="color: rgb(204, 120, 50);">int </span>t = <span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">; </span>t < j<span style="color: rgb(204, 120, 50);">; </span>t++) {
  17.                             <span style="color: rgb(204, 120, 50);">if </span>(dp[i - <span style="color: rgb(104, 151, 187);">1</span>][t] == Integer.<span style="color: rgb(152, 118, 170); font-style: italic;">MAX_VALUE</span>) <span style="color: rgb(204, 120, 50);">continue;
  18. </span><span style="color: rgb(204, 120, 50);">                            else </span>dp[i][j] = Math.<span style="font-style: italic;">min</span>(dp[i][j]<span style="color: rgb(204, 120, 50);">, </span>dp[i - <span style="color: rgb(104, 151, 187);">1</span>][t] + j - t)<span style="color: rgb(204, 120, 50);">;
  19. </span><span style="color: rgb(204, 120, 50);">                        </span>}
  20.                     }
  21.                 }
  22.             }
  23.         }

  24.         <span style="color: rgb(204, 120, 50);">int </span>min = Integer.<span style="color: rgb(152, 118, 170); font-style: italic;">MAX_VALUE</span><span style="color: rgb(204, 120, 50);">;
  25. </span><span style="color: rgb(204, 120, 50);">        int </span>end = -<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">;
  26. </span><span style="color: rgb(204, 120, 50);">
  27. </span><span style="color: rgb(204, 120, 50);">        for</span>(<span style="color: rgb(204, 120, 50);">int </span>j=<span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">; </span>j<lena<span style="color: rgb(204, 120, 50);">; </span>j++){
  28.             <span style="color: rgb(204, 120, 50);">if</span>(dp[lenb-<span style="color: rgb(104, 151, 187);">1</span>][j] < min) {
  29.                 min = dp[lenb-<span style="color: rgb(104, 151, 187);">1</span>][j]<span style="color: rgb(204, 120, 50);">;
  30. </span><span style="color: rgb(204, 120, 50);">                </span>end = j<span style="color: rgb(204, 120, 50);">;
  31. </span><span style="color: rgb(204, 120, 50);">            </span>}
  32.         }

  33. <span style="color: rgb(128, 128, 128);">//        System.out.println(end);
  34. </span><span style="color: rgb(128, 128, 128);">//        System.out.println(min);
  35. </span><span style="color: rgb(128, 128, 128);">
  36. </span><span style="color: rgb(128, 128, 128);">        </span><span style="color: rgb(204, 120, 50);">if</span>(end==-<span style="color: rgb(104, 151, 187);">1</span>) <span style="color: rgb(204, 120, 50);">return </span><span style="color: rgb(106, 135, 89);">"no match!"</span><span style="color: rgb(204, 120, 50);">;
  37. </span><span style="color: rgb(204, 120, 50);">        return </span>a.substring(end-min+<span style="color: rgb(104, 151, 187);">1</span><span style="color: rgb(204, 120, 50);">, </span>end+<span style="color: rgb(104, 151, 187);">1</span>)<span style="color: rgb(204, 120, 50);">;
  38. </span><span style="color: rgb(204, 120, 50);">    </span>}</font>
复制代码
回复

使用道具 举报

🔗
chm34 2016-1-20 05:36:09 | 只看该作者
本楼:
全局:
dp可破之。。
回复

使用道具 举报

🔗
smallpig1988 2016-1-20 06:06:16 | 只看该作者
全局:
类似于 LC 76. Minimum Window Substring。 但需要保持order。
回复

使用道具 举报

🔗
kidzlike 2016-1-20 06:11:09 | 只看该作者
全局:
我怎么觉得得用dp做?
回复

使用道具 举报

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

使用道具 举报

🔗
 楼主| neverlandly 2016-1-20 06:25:41 | 只看该作者
全局:
kidzlike 发表于 2016-1-20 06:11
我怎么觉得得用dp做?

能详细讲讲么,怎么用dp?
回复

使用道具 举报

🔗
muybienw 2016-1-20 07:11:11 | 只看该作者
全局:
上面帖子的代码格式有问题... 重新贴一下

  1.     public String findShortest(String a, String b){
  2.         if(a==null || b==null || a.length()==0 || b.length()==0) throw new IllegalArgumentException();

  3.         int lena = a.length(), lenb = b.length();
  4.         int[][] dp = new int[lenb][lena];

  5.         for(int i=0; i<lenb; i++){
  6.             char bc = b.charAt(i);
  7.             for(int j=0; j<lena; j++){
  8.                 char ac = a.charAt(j);
  9.                 dp[i][j] = Integer.MAX_VALUE;

  10.                 if(ac==bc){
  11.                     if(i==0) dp[i][j] = 1;
  12.                     else {
  13.                         for (int t = 0; t < j; t++) {
  14.                             if (dp[i - 1][t] == Integer.MAX_VALUE) continue;
  15.                             else dp[i][j] = Math.min(dp[i][j], dp[i - 1][t] + j - t);
  16.                         }
  17.                     }
  18.                 }
  19.             }
  20.         }

  21.         int min = Integer.MAX_VALUE;
  22.         int end = -1;

  23.         for(int j=0; j<lena; j++){
  24.             if(dp[lenb-1][j] < min) {
  25.                 min = dp[lenb-1][j];
  26.                 end = j;
  27.             }
  28.         }

  29. //        System.out.println(end);
  30. //        System.out.println(min);

  31.         if(end==-1) return "no match!";
  32.         return a.substring(end-min+1, end+1);
  33.     }
复制代码
回复

使用道具 举报

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

使用道具 举报

🔗
muybienw 2016-1-20 11:04:11 | 只看该作者
全局:
pattern对应到i位置,string对应到j位置时,shortest substring的长度,Int_Max表示不存在
回复

使用道具 举报

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

本版积分规则

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