剑指Offer----面试题42(2):左旋转字符串

本文介绍两种实现字符串左旋转的方法:一种使用字符串截取和拼接;另一种通过三次翻转完成旋转。并提供了详细的代码实现及测试案例。

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

题目:


字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如输入字符串“abcdefg”和数字2,该函数将返回左旋转2位得到的结果“cdefgab”。

方法一


分析


利用string字符串的substr函数截取子串,再进行黏贴。

源码

#include<iostream>
#include<string>

using namespace std;

void Function(string &str, unsigned int n)
{
	if (str.empty() || str.length() <= n || n <= 0)
	{
		cout << "不满足旋转条件" << endl;
		return;
	}

	int len = str.length();
	string bStr = str.substr(0, n);
	string aStr = str.substr(2);

	str = aStr + bStr;
}

void test1()
{
	cout << "=============test1:abcdefg, 2=================" << endl;
	string str = "abcdefg";
	Function(str, 2);
	cout << str << endl;
}

void test2()
{
	cout << "=============test1:abcdefg, 0=================" << endl;
	string str = "abcdefg";
	Function(str, 0);
	cout << str << endl;
}

void test3()
{
	cout << "=============test1:abcdefg, 8=================" << endl;
	string str = "abcdefg";
	Function(str, 8);
	cout << str << endl;
}

void test4()
{
	cout << "=============test1:空, 2=================" << endl;
	string str;
	Function(str, 8);
	cout << str << endl;
}

int main()
{
	test1();
	test2();
	test3();
	test4();
	
	system("pause");
	return 0;
}

运行结果:
=============test1:abcdefg, 2=================
cdefgab
=============test1:abcdefg, 0=================
不满足旋转条件
abcdefg
=============test1:abcdefg, 8=================
不满足旋转条件
abcdefg
=============test1:空, 2=================
不满足旋转条件

请按任意键继续. . .


方法二


分析


以“abcdefg”为例,我们可以把它分为两部分。由于想把它的前两个字符移到后边,我们可以把前两个字符分到第一部分,把后边的字符分到第二部分。分别翻转这两部分,也是得到“bagfedc”。接下来我们再翻转整个字符串,得到的“cdefgab”刚好就是把原始字符串左旋转2位的结果。

源码

#include<iostream>
using namespace std;

void Reverse(char *arr, int length)
{
	if (arr == nullptr || length <= 1)
		return;

	char *pBegin = arr;
	char *pEnd = arr + length - 1;

	while (pBegin < pEnd)
	{
		char temp = *pBegin;
		*pBegin = *pEnd;
		*pEnd = temp;

		++pBegin;
		--pEnd;
	}
}

void LeftRotateString(char *arr, int n)
{
	if (arr == nullptr || n <= 0)
	{
		cout << "字符串不满足翻转条件" << endl;
		return;
	}

	int len = static_cast<int>(strlen(arr));
	if ( n >= len)
	{
		cout << "字符串不满足翻转条件" << endl;
		return;
	}

	Reverse(arr, n);
	Reverse(arr + n, len - n);
	Reverse(arr, len);
}

void test11()
{
	cout << "=============test1:abcdefg, 2=================" << endl;
	char str[] = "abcdefg";
	LeftRotateString(str, 2);
	if (str == nullptr)
		cout << "字符串为空" << endl;
	else
		cout << str << endl;
}

void test12()
{
	cout << "=============test2:abcdefg, 0=================" << endl;
	char str[] = "abcdefg";
	LeftRotateString(str, 0);
	if (str == nullptr)
		cout << "字符串为空" << endl;
	else
		cout << str << endl;
}

void test13()
{
	cout << "=============test3:abcdefg, 8=================" << endl;
	char str[] = "abcdefg";
	LeftRotateString(str, 8);
	if (str == nullptr)
		cout << "字符串为空" << endl;
	else
		cout << str << endl;
}

void test14()
{
	cout << "=============test4:空, 2=================" << endl;
	char *str = nullptr;
	LeftRotateString(str, 2);
	if (str == nullptr)
		cout << "字符串为空" << endl;
	else
		cout << str << endl;
}

void test15()
{
	cout << "=============test5:abcdefg, 1=================" << endl;
	char str[] = "abcdefg";
	LeftRotateString(str, 1);
	if (str == nullptr)
		cout << "字符串为空" << endl;
	else
		cout << str << endl;
}

int main()
{
	test11();
	test12();
	test13();
	test14();
	test15();
	cout << endl;

	system("pause");
	return 0;
}

运行结果:
=============test1:abcdefg, 2=================
cdefgab
=============test2:abcdefg, 0=================
字符串不满足翻转条件
abcdefg
=============test3:abcdefg, 8=================
字符串不满足翻转条件
abcdefg
=============test4:空, 2=================
字符串不满足翻转条件
字符串为空
=============test5:abcdefg, 1=================
bcdefga

请按任意键继续. . .


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值