📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
楼主: wrj5518
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】6.16-6.22 CareerCup 1.5

🔗
asdw3276 2014-6-19 12:42:20 | 只看该作者
全局:
wilbert 发表于 2014-6-19 07:44
我举个例子吧, a1和111个a都能compress出a111,我们现成的这种算法是无法还原的。

是的。如果原字符串有数字就无法还原了,那如果没有数字呢
回复

使用道具 举报

🔗
heycinderella 2014-6-20 04:04:28 | 只看该作者
全局:

【解题思路】
         * Go through each character in the input string, compare it with the
         * previous one, count the frequency.
【时间复杂度】
O(n)
【空间复杂度】
O(n)
【gist link】
https://gist.github.com/XiaoxiaoLi/fa251f9f537c536a74ba
回复

使用道具 举报

🔗
daisyang 2014-6-20 07:02:41 | 只看该作者
全局:
本帖最后由 daisyang 于 2014-6-20 07:07 编辑

解题思路: 从左到右遍历string,然后数之后相同字符的个数,放到新string里面去,下次检验过的字符的下一个开始.

时间: O(n) 时间复杂度是n吧? 虽然有两个循环但是不检验重复的 
空间:O(n)
gist: https://gist.github.com/daisyang/c986085ccf159e1f8187

求帮忙检验对不对
回复

使用道具 举报

🔗
daisyang 2014-6-20 10:21:24 | 只看该作者
全局:
serolins 发表于 2014-6-20 09:23
不需要用两次scan吧,一次就够了的。

对的~~ 谢谢哈~~改了
回复

使用道具 举报

🔗
grassgigi 2014-6-20 11:25:32 | 只看该作者
全局:
本帖最后由 grassgigi 于 2014-6-20 11:27 编辑

【解题思路】
Scan through the whole string and compress, it will stop either:
1. the compress string exceed the length of original string, in which we return original one
2. finish compression and return result

【时间复杂度】
O(N) for scanning through string

【空间复杂度】
O(N) for saving the compression result

PS: There seems to be a way to do in place compression, since we can use some special character to indicate the 'gap' created by compression, and reversing back the original string if we need to return that.

【gist link】
https://gist.github.com/chrislukkk/d6a1827124ac7d47534c
回复

使用道具 举报

🔗
pud 2014-6-20 12:27:43 | 只看该作者
全局:
【解题思路】
遍历原来string,如果后一个字符和前一个字符相同,count++,如果不相同,前一个字符写到新string里,count=1。 最后比较两个string长度
【时间复杂度】
   O(n)
【空间复杂度】
不知道要怎么算?
【gist link】https://gist.github.com/yokiy/8dcfbd745317c5c9b46e
回复

使用道具 举报

🔗
heycinderella 2014-6-20 23:42:30 | 只看该作者
全局:
readman 发表于 2014-6-16 14:34
【解题思路】
  Save the first character. Iterate and compare next character, increment count
  C ...

我看大家都是compress完了最后一步比较length大小,像书里这样先扫一遍最后结果的length也要多出个O(N),如果是用StringBuffer的话,真的能减少时间么?求解谢谢!
回复

使用道具 举报

🔗
readman 2014-6-20 23:51:32 | 只看该作者
全局:
heycinderella 发表于 2014-6-20 23:42
我看大家都是compress完了最后一步比较length大小,像书里这样先扫一遍最后结果的length也要多出个O(N) ...

StringBuffer和String的区别, 请看http://www.programcreek.com/2014 ... -vs-by-constructor/

回复

使用道具 举报

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

本版积分规则

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