/************************************************************************/
/* 左旋转字符串
题目:
定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部。
如把字符串abcdef 左旋转2 位得到字符串cdefab。请实现字符串左旋转的函数。
要求时间对长度为n 的字符串操作的复杂度为O(n),辅助内存为O(1)。
*/
/************************************************************************/
#include <iostream>
using namespace std;
void Reverse(char *begin,char* end)
{
char temp;
while(begin<end)
{
temp=*begin;
*begin=*end;
*end=temp;
begin++;
end--;
}
}
char* LeftReverse(char *p,int n)
{
int length=strlen(p);
Reverse(p,p+n-1);
Reverse(p+n,p+length-1);
Reverse(p,p+length-1);
return p;
}
int main()
{
char p[]="abcdef";//此处不能定义为char* p="abcdef",因为char* p="abcdef"定义的是一个字符串常量,即const char类型的数组,其值不能被修改
//详细分析可见http://apps.hi.baidu.com/share/detail/33344482
puts(LeftReverse(p,3));
return 0;
}