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

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

🔗
zhenzhenanan 2014-6-17 08:30:59 | 只看该作者
全局:
【解题思路】
1. 首先遍历一遍字符串,数出空格的个数。如果字符串后面留的空间刚好就是转换后所需要的空间的话,这一步可以省略;
2. 如果第一步没有省略,则找到转换后的字符串最后一个字符所应该在的位置。方法也很简单,那就是原来只有一个字符的空格现在需要3个字符的空间,转换一下即可。如果第一步省略了,则字符转换后,最后一个字符的位置就是整个字符串(包括留白)的最后一个位置。
3. 从后向前遍历字符串,如果是非空格,直接就放在正确的位置;如果是空格,则转换成'%20'。
【时间复杂度】
O(n)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/seemuch/af64b140e576b6ea73ad#file-1_4-cc
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
None
回复

使用道具 举报

🔗
readman 2014-6-17 10:51:42 | 只看该作者
全局:
小柯西 发表于 2014-6-17 02:30
为什么会想到用recursion来做这道题!

最近在看算法设计....
回复

使用道具 举报

🔗
ryancooper 2014-6-17 11:36:22 | 只看该作者
全局:
habina 发表于 2014-6-16 13:16
【解题思路】
  Split the whole string by space, save each word in an array.
  Concatenate and put  ...

I think your space complexity should be O(n)
回复

使用道具 举报

🔗
habina 2014-6-17 11:57:25 | 只看该作者
全局:
ryancooper 发表于 2014-6-17 11:36
I think your space complexity should be O(n)

Thanks for pointing that out.
I think my code does not satisfy for performing operation in place.
I'll rewrite it.
Thanks again.
回复

使用道具 举报

🔗
ryancooper 2014-6-17 12:05:31 | 只看该作者
全局:
habina 发表于 2014-6-17 11:57
Thanks for pointing that out.
I think my code does not satisfy for performing operation in place. ...

That's fineActually, maybe you should use another language to write this if you want to do it in place since object string in python is immutable
回复

使用道具 举报

🔗
habina 2014-6-17 12:12:12 | 只看该作者
全局:
ryancooper 发表于 2014-6-17 12:05
That's fineActually, maybe you should use another language to write this if you want to do ...

Yeah. I totally agree about that. I'll do it in c.
回复

使用道具 举报

🔗
fang_wu 2014-6-17 18:00:00 | 只看该作者
全局:
【解题思路】
先把index变成最后一个不是空格,然后就是替换。
【时间复杂度】
O(n)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/qiangusc/def553c9b31b44560818
回复

使用道具 举报

🔗
chouclee 2014-6-17 19:35:06 | 只看该作者
全局:
【解题思路】traverse字符串s,统计空格的数量c,new 一个s.length() + c<<1 大小的char[],再次traverse,重组字符串
【时间复杂度】O(n)
【空间复杂度】O(n)
【gist link】https://gist.github.com/chouclee/d111abfd1ff20e41cd01
回复

使用道具 举报

🔗
wilbert 2014-6-18 02:17:21 | 只看该作者
全局:
【解题思路】
Original string must ends with '\0'. Count the number of spaces to get the length of the new string. Then start from the end of the new string and original string, if meets space in the original string, replace it with "%20", otherwise just move the character.
【时间复杂度】
O(N)
【空间复杂度】
O(1), in-place replace, no additional place
【gist link】
https://gist.github.com/iwilbert/d0efe909f1311c081bc2
回复

使用道具 举报

🔗
林微熙 2014-6-18 11:11:36 | 只看该作者
全局:
【解题思路】
go through all characters in String, replace space with '%20'
【时间复杂度】
O(N)
【空间复杂度】
o(1)
【gist link】https://gist.github.com/hilda8519/e4f3e5ade2e38224129a

点评

楼下code review:咱们的方法是一致的~实现起来有小小的不一样,看了你的程序,学了新的char[]转string的方法:String s=new String(char[]); :D  发表于 2014-6-23 18:00
ok.thanks  发表于 2014-6-20 11:25
I think the space complexity should be O(n), unless we assume the length of the string is no more than a number like 1000 ...  发表于 2014-6-20 04:23
回复

使用道具 举报

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

本版积分规则

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