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

[CareerCup] 【第三轮】6.23-6.29 CareerCup 1.8

🔗
grassgigi 2014-6-24 00:45:30 | 只看该作者
全局:
heycinderella 发表于 2014-6-24 00:02
求解空间为啥是O(n)?

Cuz string type is immutable, so concatenation creates new string object.
回复

使用道具 举报

🔗
zhenzhenanan 2014-6-24 01:15:03 | 只看该作者
全局:
【解题思路】
好像是CTCI原题吧。
把两个s1连接起来变成一个新的string,我们叫它s3,然后我们用isSubstring来检查s2是否是s3的substring。
原理是:如果s2是s1的rotation,比如s1是:abcdefg,s2是:defgabc。那么当我们把两个s1连接起来之后,s3是:abcdefgabcdefg,可以看见,s2肯定是s3的substring。
【时间复杂度】
O(n),n是s1的长度。因为拷贝一个string虽然可能只需要一行代码,但背后的时间是O(n)。
【空间复杂度】
O(n), where n is the length of s1
【gist link】
https://gist.github.com/seemuch/af64b140e576b6ea73ad#file-1_8-cc
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
None.
回复

使用道具 举报

🔗
wilbert 2014-6-24 01:53:16 | 只看该作者
全局:
【解题思路】
Check if s2 is the substring of s1+s1
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/iwilbert/77a2aa34ab66cdbbb4b5

点评

请问一下为啥空间复杂度是O(1)呢?  发表于 2014-8-25 02:27
回复

使用道具 举报

🔗
heycinderella 2014-6-24 05:47:46 | 只看该作者
全局:
grassgigi 发表于 2014-6-24 00:45
Cuz string type is immutable, so concatenation creates new string object.

啊果然是这样,之前没想到,谢谢!
回复

使用道具 举报

🔗
林微熙 2014-6-24 07:19:44 | 只看该作者
全局:
【解题思路】
Check if s2 is the substring of s1+s1
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】https://gist.github.com/hilda8519/34e3624fa25453ccbaa7

按书做的

点评

请问一下为啥空间复杂度是O(1)呢?  发表于 2014-8-25 02:27
回复

使用道具 举报

🔗
pud 2014-6-24 08:34:45 | 只看该作者
全局:
【解题思路】
看答案提示的,检查s2是不是s1+s1的substring
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】https://gist.github.com/yokiy/27b7f31ff00ffcc4f896
回复

使用道具 举报

🔗
fang_wu 2014-6-24 10:05:53 | 只看该作者
全局:
heycinderella 发表于 2014-6-24 00:02
求解空间为啥是O(n)?

s1+s1,就会开通s1的空间
回复

使用道具 举报

🔗
jason51122 2014-6-24 12:31:02 | 只看该作者
全局:
本帖最后由 jason51122 于 2014-6-24 12:50 编辑

【解题思路】Concat 2 str1 together as str. Find if str2 is a substring of str.
【时间复杂度】O(N^2) str.indexOf(str1) is O(N^2)
【空间复杂度】O(N) str = str1 + str1
【gist link】https://gist.github.com/jason51122/6f636b270039087a41ce
回复

使用道具 举报

🔗
jason51122 2014-6-24 13:02:41 | 只看该作者
全局:
本帖最后由 jason51122 于 2014-6-24 13:06 编辑
heycinderella 发表于 2014-6-23 08:18
【解题思路】
这个题当时想了好久都没想出来,这次看见题立刻想起书上的答案了。。。
         * Same th ...

Suppose the length of string is N. I think append() operation for StringBuffer will cost 2N, which is O(N).
回复

使用道具 举报

🔗
readman 2014-6-24 15:05:20 | 只看该作者
全局:
grassgigi 发表于 2014-6-23 02:14
【解题思路】
Concatenate s1 with itself, since there is a rotation, after concatenation s2 will be  ...

bm的空间是n
回复

使用道具 举报

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

本版积分规则

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