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

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

🔗
兰橘清檬 2014-6-17 02:49:24 | 只看该作者
全局:
【解题思路】
遍历原 string 建立新 string,比较二者长度选择返回值
【时间复杂度】
O(n)
【空间复杂度】
O(n)
【gist link】
https://gist.github.com/JoyceeLee/fcbfaf6b4100d99918ea
回复

使用道具 举报

🔗
zhenzhenanan 2014-6-17 09:22:50 | 只看该作者
全局:
【解题思路】
就是直接做,从前向后遍历。在循环的中间检查一下compressed之后的string是不是比原来的sting长,如果是的话就直接返回原来的string即可。
【时间复杂度】
O(n)
【空间复杂度】
O(n)
【gist link】
https://gist.github.com/seemuch/af64b140e576b6ea73ad#file-1_5-cc
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
None

点评

如果输入的字符串全是相同的字符,那你代码的时间复杂度就是O(n^2)了....fill the count那小段没看懂,按你的程序的话,那如果"a"出现35次,好像会填入"a53".  发表于 2014-6-17 10:12
回复

使用道具 举报

🔗
xjbTalk 2014-6-17 09:54:23 | 只看该作者
全局:
【解题思路】Compare current character and its next character, if they are different or encounter the boundary, append it and its count to result. Otherwise increase the count by 1 and move to the next character.
【时间复杂度】O(n) for going through all the characters.
【空间复杂度】O(n) for storing the result string.
【gist link】https://gist.github.com/30d8ea8624913bf7c18f.git
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
input : " ". expect : " ". input : "". expect : "".
input : Null. expect : Null.
input : "a". expect : "a"

点评

打不开git链接捏  发表于 2014-6-25 22:51
回复

使用道具 举报

🔗
fang_wu 2014-6-17 18:03:06 | 只看该作者
全局:
【解题思路】就是用计数器来计数,最后来比较大小
【时间复杂度】O(n)
【空间复杂度】O(n)
【gist link】https://gist.github.com/qiangusc/2769ad688dd4ffe386c9
回复

使用道具 举报

🔗
chouclee 2014-6-17 20:49:30 | 只看该作者
全局:
【解题思路】遍历一遍字符串,用一个boolean标记是否第一次遇到该字符,第一次遇到append进StringBuilder里,然后计数,直到遇到不同的字符,将统计的次数append进StringBuilder里,重置boolean标记。
【时间复杂度】O(n)
【空间复杂度】StringBuilder可以自动扩容,average O(n)
【gist link】https://gist.github.com/chouclee/b57b6915366b87cdff44
【test case】"aaaaaaaaaaaaaaaaaaaaaaaa" ,"aaaaabbbbb      ","","abc","aaaabbbbc","呵呵呵呵呵呵","呵呵"
回复

使用道具 举报

🔗
zhenzhenanan 2014-6-17 23:36:58 | 只看该作者
全局:
小柯西 发表于 2014-6-17 09:54
【解题思路】Compare current character and its next character, if they are different or encounter the ...

谢点评~
我的代码如果是处理全部相同字符的字符串,时间其实还是O(n)。可能我代码比较乱,不好意思~
fill the count那段确实写错了,已经修改。
回复

使用道具 举报

🔗
zhenzhenanan 2014-6-17 23:37:51 | 只看该作者
全局:
小柯西 发表于 2014-6-17 09:54
【解题思路】Compare current character and its next character, if they are different or encounter the ...

还有,应该怎么写“点评”啊?点“回复“好像不对的样子。。

点评

帖子回复旁边有个“点评”的按钮啊,难道是因为你的等级不够用不了?那个时间复杂度确实还是O(n)我看错了。。  发表于 2014-6-18 00:19
回复

使用道具 举报

🔗
asdw3276 2014-6-17 23:48:15 | 只看该作者
全局:
本帖最后由 asdw3276 于 2014-6-17 23:50 编辑

【解题思路】遍历一遍字符串,用两个char存放当前读取的字符和上一个字符。如果两者相等,计数加1。如果两者不等,写入当前计数到result中,然后将count设为0。然后更新两个char。 最后比较result和原字符串的长度。
【时间复杂度】O(n)
【空间复杂度】O(1)
【gist link】https://gist.github.com/asdw3276/8da1d8560fe3897442d8
【test case】""    " "   "a" "aa" "ab" "aaa"   "aaaaaabbbaaa" "aaaaaabbba"
感谢所有给我code review的小伙伴

点评

空间复杂度应该还是O(N),因为你还是新建了一个String  发表于 2014-6-18 03:00
回复

使用道具 举报

🔗
wilbert 2014-6-18 02:51:26 | 只看该作者
全局:
【解题思路】
compare the current character with the previous one, if equals, cnt++, otherwise, appends the previous char and cnt to the stringbuffer and updates the previous char and cnt. cnt is the occurrence of the same consecutive characters. Once done, compare the length with the original one, if not less than the original one, return the original one.
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist link】
https://gist.github.com/iwilbert/7fd763f020e29d9c482d

====================================
【Follow up Question】
Can you decompress the compressed string generated from your code? (In my case: NO), If not, how would you do that? (Thinking......)
回复

使用道具 举报

🔗
monkerek 2014-6-18 11:12:03 | 只看该作者
全局:

【解题思路】
traverse the string directly and count #characters. Then check whether the result is smaller than input string.
【时间复杂度】
O(n)
【空间复杂度】
O(n)
【gist link】
https://gist.github.com/monkerek/b2051b2d48ccbcfe1db2

点评

[来自楼下的code review]用Python写的呀~觉得好酷炫!你的for..if...else比我的双重while用得简洁清晰多了!下次我也这么写~~ 【等待你给我的code review~】  发表于 2014-6-23 20:12
回复

使用道具 举报

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

本版积分规则

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