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

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

全局:

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

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

x
1.2 Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string.

回复解法可以按照以下格式来
【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】
---------------Optional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】

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




上一篇:【第三轮】6.16-6.22 CareerCup 1.1
下一篇:【第三轮】6.16-6.22 CareerCup 1.3
推荐
readman 2014-6-16 00:07:32 | 只看该作者
全局:
【解题思路】
same to binary search ?
【时间复杂度】
n, since  i use char ary
【空间复杂度】
lg, no need to traverse all char in string
【gist link】
https://gist.github.com/gaoyike/50d11d25f2f59deb3150
【test case】

评分

参与人数 1大米 +5 收起 理由
xjbTalk + 5 时间复杂度这个不是你提醒,我就弄错了

查看全部评分

回复

使用道具 举报

全局:
本帖最后由 larry 于 2014-6-18 12:11 编辑

【解题思路】swap ith char and (len-i-1)th char
【时间复杂度】O(N)
【空间复杂度】O(1)
【gist link】https://gist.github.com/larry-liu/9d0e06ea7f6761fd32ee
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
str = malloc(256);
strcpy(str, "this is my test case");

I am not sure how to reply.... So I just write here.
It is only allowed in C99 mode to declare varible in for loop. To avoid that error message, I declare this i out of for loop.
Thanks for your reply and I think i need to compile my code in C99 mode.

点评

Instead of writing int i = 0; for(i = 0; i < (len+1)/2 ; i++), is it better to make i a loop variable: for(int i = 0; i < (len+1)/2 ; i++), it makes more sense to me.  发表于 2014-6-18 11:41
回复

使用道具 举报

推荐
habina 2014-6-16 06:10:34 | 只看该作者
全局:
【解题思路】
  Two pointers, named begin and end, begin points to the begin and towards to the end, end points the last character and towards to the begin.
  Swap characters between these two pointers until they point to the same address or begin pointer is step over by one of end pointer
【时间复杂度】
  O(N)
【空间复杂度】
  O(1)
【gist link】
  https://gist.github.com/habina/d55da65182a6622e64b5
【Test Case】
  null character
回复

使用道具 举报

🔗
atlas1017 2014-6-16 05:56:59 | 只看该作者
全局:
readman 发表于 2014-6-16 00:07
【解题思路】
same to binary search ?
【时间复杂度】

这题目就是想要用 char pointer的吧 虽然差不多= =
回复

使用道具 举报

🔗
qianhuang 2014-6-16 09:50:49 | 只看该作者
全局:
本帖最后由 qianhuang 于 2014-6-16 10:06 编辑

【解题思路】
two pointers which point the head/tail(not the '\0', but the one before it) of the string. Then swap their value, and update the pointers.
【时间复杂度】
O(n)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/qianhuang/96320b980038b7df26cd
回复

使用道具 举报

🔗
monkerek 2014-6-16 11:27:15 | 只看该作者
全局:
【解题思路】
Using 2 pointers respectively point to the start and the end of the string, swap the value they point to, then move the pointers iteratively.
【时间复杂度】
O(n)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/monkerek/100587049a945115f589
回复

使用道具 举报

🔗
jing0328 2014-6-16 13:44:18 | 只看该作者
全局:
本帖最后由 jing0328 于 2014-6-17 09:39 编辑

【解题思路】traverse the string(code written in java)
【时间复杂度】O(n)
【空间复杂度】O(n)
【gist link】https://gist.github.com/startupjing/8f8a5adc6ed5408923a1

回复

使用道具 举报

🔗
wilbert 2014-6-16 22:47:09 | 只看该作者
全局:
【解题思路】
得到字符串长度,swap首尾字符直到相遇
【时间复杂度】
O(n)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/anonymous/437c05fb726da019ff52
回复

使用道具 举报

🔗
xjbTalk 2014-6-17 00:13:35 | 只看该作者
全局:
本帖最后由 小柯西 于 2014-6-19 01:07 编辑

【解题思路】Locate the ending of the string, then swap those previous characters.  
【时间复杂度】n
【空间复杂度】1
【gist link】https://gist.github.com/e63557112ca2253e3d60.git
【test case】Empty string and null pointer.
回复

使用道具 举报

🔗
兰橘清檬 2014-6-17 01:50:20 | 只看该作者
全局:
【解题思路】
利用两个指针,开始一个指向数组头,一个指向数组尾,然后两两交换,指针同时向中间移动
【时间复杂度】
O(n)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/JoyceeLee/4e200d7a93e376415d67
回复

使用道具 举报

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

本版积分规则

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