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

[CareerCup] 【第四轮】3.30 - 4.5 Career Cup 1.2

全局:

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

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

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

请参加活动的童鞋跟帖回复自己的解法,回复请参考以下格式:

【解题思路】
【时间复杂度】
【空间复杂度】
【gist link]
【test case】(optional,如果觉得比较好,欢迎贴出来分享)

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


上一篇:【第四轮】3.30 - 4.5 Career Cup 1.1
下一篇:【第四轮】3.30 -4.5 Career Cup 1.3
推荐
Fiona杀G 2015-4-5 22:14:44 | 只看该作者
全局:
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

/*
[question]

1.2 Implement a function void reverse(char* str) in C or C++ which reverses a nullterminated string.

[solution]
use two pointers, one in the head another in the tail,
exchange the head and the tail element, head++ tail--
until when tail is smaller or euqal to head, then the loop can stop

[time complexity]
O(n)
scan every char in the string

[space complexity]
O(1)
we do this in place, so no extra space

[gist link]
https://gist.github.com/FionaT/a428c955bb145320ac0d

[test case]

*/

void reverse_str(char *str)
{
        int len = 0;
        char *p = str, temp;
       
        while(*p != NULL){
                p++;
                len++;
        }
       
        for(int i = 0, j = len - 1; i < j; i++, j--){
                temp = str[i];
                str[i] = str[j];
                str[j] = temp;
        }
       
}

int main()
{
        char str[] = "";
       
        reverse_str(str);
       
        cout<<str<<endl;
       
        return 0;
}

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

全局:
最后一天交作业啦

【解题思路】
* We used a temp variable to exchange the i character with leng-1-i character
* One way to do this is our method
* char[] chars = str.toCharArray();
* String newStr = String.valueOf(chars);
*
* We can also use Stringbuilder to change a character in a string
* StringBuilder myName = new StringBuilder("domanokz");
* myName.setCharAt(4, 'x');

【时间复杂度】
O(n)
【空间复杂度】
O(1)
【gist link]
https://gist.github.com/leonw007/4b1b10602e38f2e6b6c9

【test case】included in the above link

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

推荐
slaink 2015-3-31 23:41:58 | 只看该作者
全局:
【解题思路】
Reverse a \0 terminated char* string.

Keep two pointers, one at the beginning and the other one
at the end of this char* string. Swap the values that these
two pointers points too, and then move them to the middle by 1.

Reminder do not reverse the endding \0.

【时间复杂度】 O(n)
【空间复杂度】 O(1)
【gist link]
https://gist.github.com/bxshi/f68c47e803d5ec8bfdb5
【test case】(optional,如果觉得比较好,欢迎贴出来分享)
https://github.com/bxshi/intervi ... c/test/1_2_test.cpp

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
JamesJi 2015-3-30 20:58:22 | 只看该作者
全局:
【解题思路】
额··说的是要用C或者C++,我写的还是java的,思路有两个,一个是新建一个相同的空字符串数组,然后将初始的字符串数组按照倒序顺序加进去
思路二空间复杂度是O(1),即使使用一个temp变量,首尾来置换,慢慢往中间靠拢
【时间复杂度】
两个思路时间复杂度都是O(n),
【空间复杂度】
空间复杂度一个是O(n)一个是O(1)
【gist link]
https://gist.github.com/JamesJi9277/b3ce3778c23b5c762d5f
【test case】

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

地里匿名用户
🔗
匿名用户-1LWQT  2015-3-30 23:17:17
【解题思路】
通过temp变量进行String首尾置换。
【时间复杂度】
O(n)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/alikewmk ... -2-reversedstr-java

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
laonong15 2015-3-31 03:00:38 | 只看该作者
全局:
【解题思路】
   exchange the  NO. i  and NO.( string lenghth -i-1) by using  stringbuilder
【时间复杂度】
O(n)
【空间复杂度】
O(1)
【gist link]
https://gist.github.com/michaelniu/9b235951549bdc561320
【test case】
https://gist.github.com/michaelniu/9b235951549bdc561320

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
reality 2015-3-31 08:00:00 | 只看该作者
全局:
solution: swap i with s.length() - i - 1
time complexity: O(n)
space complexity: O(1)
gist: https://gist.github.com/jocelynz ... a8bb5d4#file-cc-1-2

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
A30041839 2015-3-31 15:09:28 | 只看该作者
全局:
[solution]
swap ith char with len-i-1th char
[time complexity]
O(n)
[space complexity]
O(1)
[gist]
https://gist.github.com/A30041839/dae06955b9fc47901aa4

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
yiyizheliu 2015-4-1 03:50:49 | 只看该作者
全局:
【解题思路】
两个指针,一个指头一个指尾,分别交换。易错点在于当字符串字符个数为偶数和奇数时,循环终止条件不同。
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/yiyizheliu/f7d82ceaa16179007418
[Test]
我这里不知道一个字符串以NULL结尾时怎么从控制台输入,所以测试时就用了其他字符代替NULL。这里有没有哪位可以有更好的测试方法?或告诉我测试的时候这个NULL是怎么输入的?

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
coperdli 2015-4-1 10:44:28 | 只看该作者
全局:
本帖最后由 coperdli 于 2015-4-1 10:49 编辑

思路:将str[i]和str[n-1-i]互换即可
时间复杂度: 从两端向中间逼近,循环n/2次,故O(n)
空间复杂度:O(1)
Code Snippet & Test Case: https://gist.github.com/coperd/84396d7f411e8cd6b999

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
mything 2015-4-1 11:10:31 | 只看该作者
全局:
本帖最后由 mything 于 2015-4-1 11:11 编辑

【解题思路】
1. 扫两遍,第一遍找到结尾位置,并计算字符串长度
   用双指针,一个从头开始(每次加一),一个从尾开始(每次减一),每步交换这两个指针对应内容,直到指针相减<= 0时停止.
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link]
https://gist.github.com/n2iw/c94ca78d61af19d434c7
【test case】(optional,如果觉得比较好,欢迎贴出来分享)
null,
"",
"a",
"ab",
"abc",
"abcd"

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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