首页 > 代码库 > 21.左旋转字符串

21.左旋转字符串

http://zhedahht.blog.163.com/blog/static/2541117420073993725873/

左旋转字符串:

k=2

AB1234 =>1234AB

思路:

AB=>BA

1234=>4321

BA4321=>1234AB

char * LeftRotateString(char *pStr,int k)
{
    if (pStr!=NULL)
    {
        int n = strlen(pStr);
        if (k>0&&n>0&&k<n)
        {
            // reverse first part
            Reverse(pStr,pStr+k-1);
            // reverse second part
            Reverse(pStr+k,pStr+n-1);
            // reverse whole part
            Reverse(pStr,pStr+n-1);
        }
    }
    return pStr;
}