Rotate a string in c++?

前端 未结 9 582
梦毁少年i
梦毁少年i 2020-12-29 00:57

I\'m looking for a way to rotate a string in c++. I spend all of my time in python, so my c++ is very rusty.

Here is what I want it to do: if I have a strin

9条回答
  •  离开以前
    2020-12-29 01:37

    At some point i was obsessed with divide and conquer, and used the following

    since it 'Divides' the problem to smaller problems my guess is that this performs better. Expert comments on complexity and memory access behavior are welcome :).

    Initial Call:
    rotate_about(rots_g, 0, i, j - 2);
    
    
    void rotate_about(char *str, int start, int pivot, int end)
    {
        if(pivot == start)
        {
            return ;
        }
        else if((pivot - start) <= (end - pivot))
        {
            int move_bytes = pivot-start;
            swap_bytes(&str[start], &str[pivot],move_bytes);
            rotate_about(str,pivot,pivot+move_bytes,end);
        }
        else
        {
            int move_bytes = end - pivot + 1;
            swap_bytes(&str[start], &str[pivot],move_bytes);
            rotate_about(str, start+move_bytes ,pivot,end);
        }
    }
    

提交回复
热议问题