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

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

🔗
水逼一枚 2014-8-25 04:13:49 | 只看该作者
全局:
serolins 发表于 2014-6-23 09:40
【解题思路】
// So first think about the definition of Rotations
// first of all, the two string ...

请问两个问题:
1. 即使用了StringBuilder或者是StringBuffer为啥时间复杂度是O(1)呢?我觉得就算用了StringBuilder虽然在连接字符串的时候不用new新对象,但是仍然需要进行字符串copy工作啊,应该还是O(n)啊。

2. 为啥空间复杂度是O(1)呢?不应该也是O(n)
回复

使用道具 举报

🔗
sanguine 2014-9-8 00:16:25 | 只看该作者
全局:
Idea: Same idea as the Cracking the Coding Interview book.

First, if the s2 is the rotation of s1, they should have the same length.
Then, s2 should be a subString of s1 + s1
Time Complexity: O(n)
Space Complexity: O(n)

Code Link: http://www.jyuan92.com/post-371
回复

使用道具 举报

🔗
sanguine 2014-9-8 00:18:40 | 只看该作者
全局:
serolins 发表于 2014-6-22 20:40
【解题思路】
// So first think about the definition of Rotations
// first of all, the two string ...

我觉得用了Stringbuffer应该还是O(n)的time Complexity,因为在这道题中,String和StringBuffer没有区别,String的concentrate也只进行了一次……仅仅new了一个新的String而已……

There is no different about the Time and Space Complexity when using StringBuffer instead of String. Because each String concatenation has cost equal to the length of the combined string, but each StringBuilder append has (amortized) cost equal to the length of the appended string. A single String concatenation, as use here, is always linear-time, but it’s wise to switch to a StringBuilder for repeated appends, to avoid repaying the cost of the first characters many times over.
回复

使用道具 举报

🔗
sanguine 2014-9-8 00:21:28 | 只看该作者
全局:
锦木千束 发表于 2014-6-23 06:06
【解题思路】首先检查俩字符串等长,然后检查S2是不是S1+S1的子串
【时间复杂度】不考虑isSub ...

isSubString的实现可以用String.contains,但是Oracle好像没有给String.contains的源代码?

如果用KMP的方法,好像Time Complexity是O(n)
如果用naive implementation,就是O(N^2)了
回复

使用道具 举报

🔗
sanguine 2014-9-8 00:23:03 | 只看该作者
全局:
wilbert 发表于 2014-6-23 12:53
【解题思路】
Check if s2 is the substring of s1+s1
【时间复杂度】

空间复杂度应该是O(n)吧,n决定于s1的长度
回复

使用道具 举报

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

本版积分规则

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