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

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

🔗
jwzxgo 2013-2-18 18:43:39 | 只看该作者
全局:
code in C https://github.com/wangtz/Career ... sAndStrings/Ch1Q2.c

use two points indicating start and end. swap the content while end>start
回复

使用道具 举报

🔗
Kimurate 2013-2-18 20:01:32 | 只看该作者
全局:
C语言:https://gist.github.com/hukun01/4976870
两种方法:1.直接交换第1个和倒数第1个char,第2个和倒数第2个char……
2.用栈。(这里我的strStack不是terminated string)

点评

用栈的想法不错, 不过答案建议最好do it in place  发表于 2013-2-19 10:19
回复

使用道具 举报

🔗
Kimurate 2013-2-18 20:12:28 | 只看该作者
全局:
jwzxgo 发表于 2013-2-18 18:43
code in C https://github.com/wangtz/CareerCup/blob/master/ch1ArraysAndStrings/Ch1Q2.c

use two poi ...

我的看法:
题目要求 reverse 函数返回值为 void;
指向数组尾的指针可以用 strlen 获得;
main 函数返回值一般是0,非0的一般是错误码(code style)。

从你这发现我的代码有悬挂指针的问题,赶紧改去。
回复

使用道具 举报

🔗
amoscoder 2013-2-18 22:29:30 | 只看该作者
全局:
本帖最后由 amoscoder 于 2013-2-18 22:38 编辑

思路:
       strlen()获取字符串长度后,依次交换第i个和倒数第i个字符。

https://gist.github.com/KuangyinWang/4977768

我想问下,为啥我的超链接不显示,我添加了链接。















回复

使用道具 举报

🔗
jerrysun 2013-2-18 22:41:09 | 只看该作者
全局:
https://gist.github.com/JerrySun363/3062e04ab8558830ae78

1. strlen() to find length
2. Treat it as array and run a for-loop till the half of the array

手边没有C的ide,几乎没写过C,请多多包涵。

点评

下次记得发布为public gist……  发表于 2013-2-19 10:44
回复

使用道具 举报

🔗
jerrysun 2013-2-18 22:51:15 | 只看该作者
全局:
本帖最后由 jerrysun 于 2013-2-18 22:54 编辑

和楼上思路基本一样,唯一不同的是一个在循环里声明临时变量,一个在循环外,似乎没影响吧。

回复

使用道具 举报

🔗
 楼主| Fanyare 2013-2-19 10:14:35 | 只看该作者
全局:
https://github.com/quantumrose/C ... aster/1.2.reverse.c

找到字符串末尾后逐个交换首尾字符......
回复

使用道具 举报

🔗
Kimurate 2013-2-19 10:23:40 | 只看该作者
全局:
能具体讲讲答案的 do it in place 是指什么吗?不太明白

点评

我的理解是尽量在原有的字符串上操作, 避免新开空间  发表于 2013-2-19 10:32
回复

使用道具 举报

🔗
nickmyself 2013-2-19 12:04:11 | 只看该作者
全局:
C++
先strlen(str)-1 取得字符串长度size(-1是因为 最大的的元素的index是size -1)
然后 i从0开始递增到(包括)size/2 每次执行str[i]与str[size-i]的交换

https://gist.github.com/nickmyself/4982928

第一次参加 好紧张.......
想了好久想不出新的..第一反应是用stack后来觉得每个元素都要push和pop一遍也省不了多力诶
回复

使用道具 举报

🔗
paradox 2013-2-19 15:21:50 | 只看该作者
全局:
strlen(s) find the length of the array
char* end = a + length; //directly jump to the end
...
rest are the same with other people
if you want to, can do xor swap for fanciness, but not necessary.

https://github.com/lyyjohn45/CP150_Strings/blob/master/reverse.c
回复

使用道具 举报

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

本版积分规则

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