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

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

🔗
asdw3276 2014-6-19 02:55:35 | 只看该作者
全局:
【解题思路】
原地改char数组,根据原来字符串的长度和空格的个数。
【时间复杂度】
O(N)
【空间复杂度】
o(1)
【gist link】https://gist.github.com/asdw3276/bcac142e15ec2b9193e1
回复

使用道具 举报

🔗
jyh橘子 2014-6-19 07:18:27 | 只看该作者
全局:
【解题思路】
I use a two scan approach. In the first scan, I count the number of spaces in the string, with which we can calculate the real length of the string we want. In the second scan, we replace white space with "%20". We need to  start from the end and work backward to avoid overwriting and losing information.
【时间复杂度】
O(n)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/jyhjuzi/dc69612f9ef6be06b224
回复

使用道具 举报

🔗
guchang 2014-6-19 12:24:00 | 只看该作者
全局:
本帖最后由 guchang 于 2014-6-19 12:25 编辑

【解题思路】
用char[]数组 array,从后往前遍历,遇到第一个不是空格的字符的位置,标记为真实长度(实际上是真实长度-1)。
然后从前往后遍历,每遇到空格,整体向后移动两位,空格替换为"%20",长度+2;
最后从array中截取真实长度值(此时要加回1)。
【时间复杂度】
O(N),,,妈蛋不会算。应该是错的,求解答
【空间复杂度】
O(1)
代码地址
https://gist.github.com/guchang/fd3870b62cac0d7d1db4
回复

使用道具 举报

🔗
zZ-IT 2014-6-19 13:41:30 | 只看该作者
全局:
求解,题目的意思不太明确。
You may assume that the string has sufficient space at the end of the string to hold the additional characters, 这句话 ,还有给的例子:
Input: "Mr John Smith       "
Output: "Mr%20Dohn%20Smith"  
Smith 后面结尾的空格不用转换为%20 了嘛? 题目中 U may assume那句话什么意思? 看大家写的 基本都是Smith后面的空格也要转换的吧?
回复

使用道具 举报

🔗
heycinderella 2014-6-20 01:24:16 | 只看该作者
全局:
本帖最后由 heycinderella 于 2014-6-20 02:24 编辑

【解题思路】
*Scan first to get the number of spaces. Then iterate from the back to front in the true string, place the characters at the right place
         * O(n) time, O(1) space
         */
【时间复杂度】
O(N)
【空间复杂度】
o(1)
【gist link】https://gist.github.com/XiaoxiaoLi/12fda96630d3ba820042
回复

使用道具 举报

🔗
zZ-IT 2014-6-20 01:31:57 | 只看该作者
全局:
heycinderella 发表于 2014-6-20 01:24
【解题思路】
Iterate the chars from back to front, move each non-space char to the
         * back, tu ...

求问,为什么测试数据里面最后都加了 \0 ?
回复

使用道具 举报

🔗
heycinderella 2014-6-20 02:25:14 | 只看该作者
全局:
zZ-IT 发表于 2014-6-20 01:31
求问,为什么测试数据里面最后都加了 \0 ?

sorry我题意理解错了,不应该有那个。修改后的在这https://gist.github.com/XiaoxiaoLi/12fda96630d3ba820042
回复

使用道具 举报

🔗
pud 2014-6-20 02:33:21 | 只看该作者
全局:
【解题思路】convert string to list, go through every character, replace space with'20%'
【时间复杂度】O(N)
【空间复杂度】O(1)
【gist link】https://gist.github.com/yokiy/01c7f6d7eaa3c855652a
回复

使用道具 举报

🔗
bitcpf 2014-6-20 04:21:51 | 只看该作者
全局:
【解题思路】Traverse the string, get the number of space. generate a new char array whose length is the origin length + 2*the number of space, then copy the origin string to the new char array and replace space with "%20".
【时间复杂度】O(n)
【空间复杂度】O(n)
【gist link】https://gist.github.com/bitcpf/95300d061a63c5e32f86
回复

使用道具 举报

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

本版积分规则

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