Code: Reverse A Sentence in Place

博客围绕微软一道面试题,探讨原地反转句子的方法。给出了两种实现代码,第一种时间复杂度大,第二种先整体反转再逐个反转单词。通过代码展示了具体实现过程,并给出了输出示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    这好像是传说中微软的一道面试题,没事做来玩玩。第一个方法想的比较ft,从时间复杂度上来看太大了;刚开始想其实是想的第二个方法,只是被第二个方法中把每个word都搞反了而郁闷了一下,其实再reverse一下每个word就纠正过来了:)

None.gif// ReverseInPlace.cpp : Defines the entry point for the console application.
None.gif

None.gif#include 
"stdafx.h"
None.gif#include 
"string.h"
None.gif
None.gif
#define SPACE    32
None.gif
None.gif
void SwapInPlace(char * a, char * b)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
if ( *!= *b )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
*= *^ *b;
InBlock.gif        
*= *^ *b;
InBlock.gif        
*= *^ *b;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
void ReverseInPlace(char* src)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    size_t len 
= strlen(src);
InBlock.gif    
for ( size_t i=0 ; i < len ; ++i )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( *(src+i) == SPACE )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for ( size_t m=0 ; m <= i-1 ; ++m )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for ( size_t n=0 ; n < len-1 ; ++n )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    SwapInPlace(src
+n, src+(len+n+1)%len);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            len 
-= i;
InBlock.gif            
for ( size_t n=0 ; n < len-1 ; ++n )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SwapInPlace(src
+n, src+(len+n+1)%len);
ExpandedSubBlockEnd.gif            }

InBlock.gif            len
--;
InBlock.gif            i 
= -1;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
void ReverseInPlace2(char* src)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    size_t i, start;
InBlock.gif    size_t len 
= strlen(src);
InBlock.gif    
for ( i=0 ; i < len/2 ; ++i )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SwapInPlace(src
+i, src+len-1-i);
ExpandedSubBlockEnd.gif    }

InBlock.gif    start 
= 0;
InBlock.gif    
for ( i=0 ; i < len ; ++i )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( *(src+i) == SPACE || i == len-1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( i== len-1 ) i++;
InBlock.gif            
for ( size_t j=0 ; j < (i-start)/2 ; ++j )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SwapInPlace(src
+start+j, src+i-1-j);
ExpandedSubBlockEnd.gif            }

InBlock.gif            start 
= i+1;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
int _tmain(int argc, _TCHAR* argv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
char str[] = "Reverse A Sentence In Place Absolutely a a";
InBlock.gif    printf(
"SRC: %s\r\n", str);
InBlock.gif    ReverseInPlace(str);
InBlock.gif    printf(
"DES: %s\r\n", str);
InBlock.gif    ReverseInPlace2(str);
InBlock.gif    printf(
"SRC: %s", str);
InBlock.gif    
InBlock.gif    getchar();
ExpandedBlockEnd.gif}

output:
None.gifSRC: Reverse A Sentence In Place Absolutely a a
None.gifDES: a a Absolutely Place In Sentence A Reverse
None.gifSRC: Reverse A Sentence In Place Absolutely a a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值