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

[CareerCup] [第二轮] 2/18-2/24 CareerCup 1.4

全局:

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

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

x
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 you can perform this operation in place.)

EXAMPLE
Input  : "Mr John Smith    "
Output: "Mr%20John%20Smith"

发帖规范:
http://www.1point3acres.com/bbs/thread-48094-1-1.html
http://www.1point3acres.com/bbs/thread-32423-1-1.html

评分

参与人数 1大米 +10 收起 理由
Kimurate + 10 ~

查看全部评分


上一篇:[第二轮] 2/18-2/24 CareerCup 1.3
下一篇:[第二轮] 2/18-2/24 CareerCup 1.5
🔗
edussx 2013-2-17 15:56:21 | 只看该作者
全局:
overview:
1. Create a new empty string
2. Go through the given string character by character,
     i.  If there is an non-space character, then just add it to the new string
     ii. If there is an space character, then add "%20" to the new string instead
3. Return the new string when loop ends

https://gist.github.com/edussx/4970604
回复

使用道具 举报

🔗
yang_cs 2013-2-17 17:49:33 | 只看该作者
全局:
edussx 发表于 2013-2-17 15:56
overview:
1. Create a new empty string
2. Go through the given string character by character,

他的意思应该是 in place?

所以
1. Read the string and compute the new length of this string if we replace it
2.
for (int i=newLen,j=oldLen;j>=0;--j){
  if (str[j]==' ') str[i--]='0',str[i--]='2',str[i--]='%';
  else str[i--]=str[j]
}

这样大概就可以了吧..
回复

使用道具 举报

🔗
grassgigi 2013-2-17 19:13:33 | 只看该作者
全局:
假设string长度为n, str[1]到str[m]为原来的字符,从str[n-1]开始向前扫描,到发现str[m]为止,distance = n-1-m就是str[m]要往后移动的距离
往前继续扫描的过程中,假设str[j]是空格,就把“%20”插入到str[j+distance]的为止,由于多插入了两个字符,所以distance = distance-2,以此类推..

https://gist.github.com/chrislukkk/4971010
回复

使用道具 举报

🔗
jimwallet 2013-2-17 20:53:39 | 只看该作者
全局:
create another array with new len which is equal to length of original input + 2*space number. copy characters from end of array to the new array.

https://gist.github.com/njcongtou/4971371
回复

使用道具 举报

🔗
Arthur2012 2013-2-17 23:44:24 | 只看该作者
全局:
Step 1: count the length of new string, length_new_string = strlen(str) + num_space * 2;
Step 2: replace all spaces in origin string by two cursors. if str[j] equals ' ',  str[i, i-1, i-2] are replaced by “%20” else str[i] is replaced by origin element.
https://github.com/Tedatworking/ ... ster/20130217_4.cpp
回复

使用道具 举报

🔗
Arthur2012 2013-2-17 23:48:07 | 只看该作者
全局:
jimwallet 发表于 2013-2-17 20:53
create another array with new len which is equal to length of original input + 2*space number. copy  ...

写的挺清晰的,就是我感觉在原来的string上就能转换。
回复

使用道具 举报

🔗
mazl123321 2013-2-18 04:08:47 | 只看该作者
全局:
Solution 1: implement without in place by using build in String func
Solution 2: in place.
       o examine the string backward.
       o if not space, just copy to back.
       o else insert '0' '2' '%'
https://gist.github.com/mazl123321/4973151
回复

使用道具 举报

🔗
cqx83 2013-2-18 06:38:57 | 只看该作者
全局:
https://gist.github.com/cqx83/4973856
先扫一遍String,数空格数量,定义char[]的长度,然后逐个扫入char[]中,遇到空格变为%20.
O(n)
回复

使用道具 举报

🔗
ac/dc 2013-2-18 15:19:24 | 只看该作者
全局:
1. Sort & Check (Time: n* log(n), Space: log(n))
2. Hashtable (Time: n, Space: 256)

https://gist.github.com/JasonGitHub/4975606
回复

使用道具 举报

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

本版积分规则

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