查看: 8973| 回复: 67
跳转到指定楼层
上一主题 下一主题
收起左侧

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

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 wrj5518 于 2014-6-15 23:19 编辑

1.4 Write a method to replace all spaces in a string with'%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)   
EXAMPLE
Input: "Mr John Smith       "
Output: "Mr%20Dohn%20Smith"  
         
回复解法可以按照以下格式来
【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】
---------------optional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】


Notice:
1、记得在程序注释中表明自己算法的时间、空间复杂度
2、代码难懂之处加注释
3、每道题目有对应的帖子,除了贴解法,欢迎讨论,集思广益
4、任何未尽之处,欢迎回报名帖提问,我会进一步作出修改。




上一篇:【第三轮】6.16-6.22 CareerCup 1.3
下一篇:【第三轮】6.16-6.22 CareerCup 1.5
全局:

【解题思路】
遍历 char 数组计算新数组的长度;
从后往前copy,并将空格改为 %20
【时间复杂度】
  O(n)
【空间复杂度】
  O(1)
【gist link】
https://gist.github.com/JoyceeLee/60a6d8d88866845f4461
回复

使用道具 举报

推荐
qianhuang 2014-6-16 10:10:43 | 只看该作者
全局:
【解题思路】
since the string in c++ STL is mutable, it is very easy to solve this problem.
If we can not use string. First count the number of space in the string, then we assign a new string size of (size(oldstring)+count(space)), and we scan from the end of string, when we meet a space, we replace it by "%20".
【时间复杂度】
O(n)
【空间复杂度】
O(n)
【gist link】
https://gist.github.com/qianhuang/8d23c5c0309ea246d470
回复

使用道具 举报

推荐
donnice 2014-6-29 05:56:22 | 只看该作者
全局:
donnice 发表于 2014-6-29 05:35
【解题思路】
把所有的string转化为char,碰到空格就转化为'%20'(感谢沙发给予的启发)
【时间复杂度】
...

public class Stringtest{
        public static void main(String[] args){
                String t = "da ben dan sha bi a";
                int p = 0;
                for(int n = 0; n<t.length();n++){
                        if(t.charAt(n) == ' ')
                                p++;
                }
                int k = t.length()+2*p+1;
                char[] result = new char[k];
                int j = 0;
                for(int i = 0; i<t.length();i++){
                        if(t.charAt(i)!=' '){
                                result[j] = t.charAt(i);
                                j++;
                        }
                        else{
                                result[j] = '%';
                                result[j+1] = '2';
                                result[j+2] = '0';
                                j =j+3;
                        }
                }
                for(int m = 0; m<=j; m++)
                        System.out.print(result[m]);
        }
}
楼上有些错误,不好意思……
回复

使用道具 举报

🔗
atlas1017 2014-6-16 06:36:37 | 只看该作者
全局:
【解题思路】
先计算需要空间 用char[] 存放 然后转回 String
【时间复杂度】
N
【空间复杂度】
N
【gist link】
https://gist.github.com/atlas1017/d45f5885a83bbfefb800
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
回复

使用道具 举报

🔗
readman 2014-6-16 12:06:43 | 只看该作者
全局:
【解题思路】
go through all elems in String, replace space with %20

first time write this in rec
【时间复杂度】
n
【空间复杂度】
n
【gist link】
https://gist.github.com/gaoyike/9a7198ada98a932e37b7
回复

使用道具 举报

🔗
habina 2014-6-16 13:16:37 | 只看该作者
全局:
【解题思路】
  Split the whole string by space, save each word in an array.
  Concatenate and put "%20" in between each word
【时间复杂度】
  O(N)
【空间复杂度】
  O(1)
【gist link】
https://gist.github.com/habina/cb4d5c96bc98ac87b26c
回复

使用道具 举报

🔗
monkerek 2014-6-16 21:43:33 | 只看该作者
全局:
【解题思路】
first find the index where the actual string ends
then calculate the number of spaces to be converted to assure the length of the result string
use two pointers, one pointing to the end of the former string, the other pointing to the end of the result string
then move pointers backwards to convert spaces one by one, and this can be done in-place.
【时间复杂度】
  O(n)
【空间复杂度】
  O(1)
【gist link】
  https://gist.github.com/monkerek/61cbc179c97c5d263beb
回复

使用道具 举报

🔗
jing0328 2014-6-16 22:56:27 | 只看该作者
全局:
【解题思路】first find the "true length" of the string, then whenever we find a space, we count how many spots for this space and then shift the char[] left/right by corresponding amount of spots and then replace space by %20 (感觉自己写的code很复杂, 求各位大神指点, 小弟第一次刷...)
【时间复杂度】O(n)
【空间复杂度】O(1)
【gist link】https://gist.github.com/startupjing/65f591967d74c78bc2d4
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】case when multiple spots in a space, like "Mr    John  Smith           "
回复

使用道具 举报

🔗
xjbTalk 2014-6-17 02:27:00 | 只看该作者
全局:
【解题思路】Traverse the array. Move every character into the result array by order. Insert "%20" when encounter a " ".
【时间复杂度】O(n) for traversing array once.
【空间复杂度】O(n) for tripling the size of the original array.
【gist link】https://gist.github.com/a833f1d440a33843c0ad.git

求好的test case
回复

使用道具 举报

🔗
xjbTalk 2014-6-17 02:30:51 | 只看该作者
全局:
readman 发表于 2014-6-16 12:06
【解题思路】
go through all elems in String, replace space with %20

为什么会想到用recursion来做这道题!
回复

使用道具 举报

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

本版积分规则

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