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

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

🔗
daisyang 2014-6-20 04:34:26 | 只看该作者
全局:
具体思路是 先计算出string中的空格数, 然后从后往前找把空格替换
btw, 这里的 "true " length 指的是 不包括char array 最后的'\0'吧?


时间: O(n)
空间: O(1)
gist: https://gist.github.com/daisyang/523e69a4e599a7e96f02
回复

使用道具 举报

🔗
Neal 2014-6-20 07:04:54 | 只看该作者
全局:
【解题思路】Calculate the new length of the string, then starting from the original end of the array, push each char to the new end. if having space, push "%20" instead.
【时间复杂度】O(n)
【空间复杂度】O(1)
【gist link】https://gist.github.com/nealhu/e89e058b182a3fe24d88
回复

使用道具 举报

🔗
RealityPC 2014-6-20 08:14:15 | 只看该作者
全局:
Solution 1
【解题思路】scan the original string, find the number of space and calculate the new length of string, push new char to the end of the original string to avoid overriding the original string
【时间复杂度】O(n)
【空间复杂度】O(1)
Solution 2
【解题思路】do the same thing excepting allocating new memory for the new string.
【时间复杂度】O(n)
【空间复杂度】O(n)
【gist link】https://gist.github.com/pchong90/cd2840e67a2d7621171a
回复

使用道具 举报

🔗
grassgigi 2014-6-20 10:26:55 | 只看该作者
全局:
【解题思路】
Scan from the end of the string, and replace white space in place with the target chars '%20'

The reason that can't scan from the beginning is the replace chars have larger size then single white space character, the chars following white space will be overwritten by the replace chars.

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

【空间复杂度】
O(1) in place replacement

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

使用道具 举报

🔗
jason51122 2014-6-20 12:58:20 | 只看该作者
全局:
【解题思路】Count the number of chars needed to add and get the new length. One pointer to the old end and one pointer to the new end go back at the same time. Keep moving the char from old string to new string and replacing space until it reaches the start point or there is no space to replace.
【时间复杂度】O(N)
【空间复杂度】O(1)
【gist link】https://gist.github.com/jason51122/7e89b34c8802f1b947fc
回复

使用道具 举报

🔗
tonygxxx1212 2014-6-20 13:27:01 | 只看该作者
全局:
【解题思路】Count space, calculate new length, extend char array length, replace backward
【时间复杂度】O(N)
【空间复杂度】O(1)
【gist link】https://gist.github.com/xun-gong/999e81b5eb89db67c2e6
回复

使用道具 举报

🔗
锦木千束 2014-6-21 09:39:37 | 只看该作者
全局:
【解题思路】遍历数组求得替换后的长度,然后再遍历一次,逐字copy待替换的数组,如果遇到space就替换
【时间复杂度】O(n)
【空间复杂度】O(n)
【gist link】https://gist.github.com/weazord/5de88a5bda58681892c7

本来第一反应是用链表。。。。
回复

使用道具 举报

全局:
【解题思路】
go through all characters in String, replace space with '%20'
【时间复杂度】
O(N)
【空间复杂度】
o(n)
【gist link】https://gist.github.com/tangxukai/34a0bc655a7c4b8ec3dc
回复

使用道具 举报

🔗
Tsien 2014-6-21 23:02:07 | 只看该作者
全局:

【解题思路】
遍历该字符串,判断字符是否为空,若是则替换‘%20’;否则复制该字符
【时间复杂度】
O(N)
【空间复杂度】
o(n)
【gist link】
https://gist.github.com/Tsien/a39a2edf6c4535eba329
回复

使用道具 举报

🔗
七00夜 2014-6-21 23:42:35 | 只看该作者
全局:
【解题思路】
字符串->字符数组,统计空格次数,据此扩容一个新的字符数组,顺次遍历,没有空格,则正常给新数组赋值,一旦出现空格,依次给新数组赋值‘%’,‘2’,‘0’
【时间复杂度】

【空间复杂度】

【gist link】
https://gist.github.com/498b511643a44ea36d55.git
回复

使用道具 举报

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

本版积分规则

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