| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1591 人关注过本帖
标题:一个面试题: reverse words in place with O(n) time
只看楼主 加入收藏
jianweichief
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2007-7-18
收藏
 问题点数:0 回复次数:12 
一个面试题: reverse words in place with O(n) time
将输入的字符串中的单词从后到前重排
如:输入"Somewhere i belong."
输出"Belong.i Somewhere"
请高手展示代码

[此贴子已经被HJin于2007-7-26 0:03:15编辑过]

搜索更多相关主题的帖子: reverse place words time Somewhere 
2007-07-22 17:28
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 
这算什么面试题……
我还是先问一下,为什么输出的第一个B变成大写了,而后面的S没有变成小写,具体规则是什么?

[此贴子已经被作者于2007-7-23 1:15:41编辑过]

2007-07-23 01:09
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
呵呵~

Fight  to win  or  die...
2007-07-23 14:52
huawang99
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2007-1-28
收藏
得分:0 
呵呵,学习中……

2007-07-23 17:57
jianweichief
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2007-7-18
收藏
得分:0 

规则就是这样,所以才提的问啊.


2007-07-25 21:19
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
:输入"Somewhere i belong."
输出"Belong.i Somewhere"


那是你的笔误吧,第二句怎么把belong的b大写了?
这个简单的颠倒字符串,用stack可以解决。

Fight  to win  or  die...
2007-07-25 21:48
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(jianweichief)一个面试题: reverse words in...

/*---------------------------------------------------------------------------
File name: ReverseWords.c
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 7/25/2007 09:05:10
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762


Modification history:
===========================================================================

1. I wrote this function a few years ago. Fully test it before delivering it
to your client.
2. I removed comments to force you study the code instead of copy it.
3. Good luck!
*/

#include <stdio.h>
#include <string.h>

void strsrev(char* p, char* r)
{
char t;
while(p<r)
{
t = *p;
*p++ = *r;
*r-- = t;
}
}

/**
Reverse words in place.

O(n) algorithm.
*/
void wordrev(char* s)
{
char*b =s, *e = s+strlen(s)-1;

if(!s)
return;

strsrev(b, e);
b=e=s;

while(*b)
{
while(*b && *b == ' ')
++b;

e = b;
while(*e && *e != ' ')
++e;

strsrev(b, e-1);

b=e;
}
}


int main()
{
char s[100] = "Somewhere i belong.";

wordrev(s);
*s &= 0xDF;

printf("%s\n", s);

return 0;
}

[此贴子已经被作者于2007-7-26 0:07:08编辑过]


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-07-26 00:05
jianweichief
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2007-7-18
收藏
得分:0 
以下是引用HJin在2007-7-26 0:05:54的发言:

/*---------------------------------------------------------------------------
File name: ReverseWords.c
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 7/25/2007 09:05:10
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762


Modification history:
===========================================================================

1. I wrote this function a few years ago. Fully test it before delivering it
to your client.
2. I removed comments to force you study the code instead of copy it.
3. Good luck!
*/

#include <stdio.h>
#include <string.h>

void strsrev(char* p, char* r)
{
char t;
while(p<r)
{
t = *p;
*p++ = *r;
*r-- = t;
}
}

/**
Reverse words in place.

O(n) algorithm.
*/
void wordrev(char* s)
{
char*b =s, *e = s+strlen(s)-1;

if(!s)
return;

strsrev(b, e);
b=e=s;

while(*b)
{
while(*b && *b == ' ')
++b;

e = b;
while(*e && *e != ' ')
++e;

strsrev(b, e-1);

b=e;
}
}


int main()
{
char s[100] = "Somewhere i belong.";

wordrev(s);
*s &= 0xDF;

printf("%s\n", s);

return 0;
}


红字部分看的不是很懂啊,当第一次循环结束后,b和e应该都指向.belong中的l的位置吧。然后执行while(*b && *b == ' ') ++b;语句,b能指向i吗?
还有e刚执行完第一次循环后,不是也指向l吗,如何在第二次调用strsrev(b, e-1);时,指向i呢?


2007-08-01 22:18
medicihophy
Rank: 1
等 级:新手上路
威 望:1
帖 子:102
专家分:0
注 册:2007-7-28
收藏
得分:0 
以下是引用HJin在2007-7-26 0:05:54的发言:

/*---------------------------------------------------------------------------
File name: ReverseWords.c
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 7/25/2007 09:05:10
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762


Modification history:
===========================================================================

1. I wrote this function a few years ago. Fully test it before delivering it
to your client.
2. I removed comments to force you study the code instead of copy it.
3. Good luck!
*/

#include <stdio.h>
#include <string.h>

void strsrev(char* p, char* r)
{
char t;
while(p<r)
{
t = *p;
*p++ = *r;
*r-- = t;
}
}

/**
Reverse words in place.

O(n) algorithm.
*/
void wordrev(char* s)
{
char*b =s, *e = s+strlen(s)-1;

if(!s)
return;

strsrev(b, e);实现字符串倒序
b=e=s;

while(*b)
{
while(*b && *b == ' ')
++b;用于跳出多余的空格

e = b;
while(*e && *e != ' ')
++e;e指向空格

strsrev(b, e-1);将一个单词倒序

b=e;
}
}


int main()
{
char s[100] = "Somewhere i belong.";

wordrev(s);
*s &= 0xDF;首字符大写

printf("%s\n", s);

return 0;
}


比较精炼的程序,没有用到栈,对了,这个首字符大写是我猜的,哪里有写?经验不足啊!

[此贴子已经被作者于2007-8-2 12:29:06编辑过]


2007-08-02 12:26
medicihophy
Rank: 1
等 级:新手上路
威 望:1
帖 子:102
专家分:0
注 册:2007-7-28
收藏
得分:0 
明白了,ASII与就可以实现了,比较有经验啊!

2007-08-02 12:41
快速回复:一个面试题: reverse words in place with O(n) time
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021203 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved