【数据结构和算法面试题】左旋转字符串_问题分析

问题分析:本题是常见的旋转字符串的问题,解决的方法是两步旋转的方法:

【数据结构和算法面试题】左旋转字符串_数据结构_02

方法:

void do_reverse(char *p_start, char *p_end){
	if (NULL == p_start || NULL == p_end || p_start > p_end) return;
	char tmp;
	while(p_start < p_end){
		tmp = *p_start;
		*p_start = *p_end;
		*p_end = tmp;
		p_start ++;
		p_end --;
	}
}

void reverse(char *s, int len){
	if (NULL == s || len == 0) return;

	char *p1_start = s;
	char *p2_end = p1_start + strlen(s) - 1;

	char *p1_end = p1_start;
	int index = 1;
	while (index < len){
		index ++;
		p1_end ++;
	}
	char *p2_start = p1_end + 1;
	do_reverse(p1_start, p1_end);
	do_reverse(p2_start, p2_end);
	do_reverse(p1_start, p2_end);
}