5th__189. Rotate Array__【400_LeetCode之路】

本文提供了一种使用队列实现数组旋转的方法,通过将数组尾部元素移至队列并重新插入数组头部来完成旋转,该方法时间复杂度为O(n^2),空间复杂度为O(n)。

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

 解法一:

/// Source : https://leetcode.com/problems/rotate-array/description/
/// Author : liuyubobobo
/// Time   : 2018-08-10

#include <iostream>
#include <vector>
#include <queue>

using namespace std;





/// Using Queue
/// Time Complexity: O(n^2)
/// Space Complexity: O(n)



void print_queue(queue<int>& q1) {
	queue<int> q2 = q1;
int n = q2.size();
int e_cout_for_queue;
for (int j = 0; j < n; j++)
{
	e_cout_for_queue = q2.front();
	q2.pop();
	cout << e_cout_for_queue << " ";
}

}





//打印vector,不需要理解,会用就行
void print_vec(const vector<int>& vec) {
	for (int e : vec) //逐个把vec中的值赋给e,虽然不懂,但感觉好牛的样子
		cout << e << " ";
	cout << endl;
}


//vector 不能删掉首元素,queue可以通过q.pop()删除第一个元素 
//queue.front(),即最早被压入队列的元素。
//queue.back(),即最后被压入队列的元素。
//queue.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
//queue.push(x); 将x 接到队列的末端。

//末尾删除元素: vec.pop_back();
//任意位置插入元素: vec.insert();
class Solution {
public:
	void rotate(vector<int>& nums, int k) {

		queue<int> q;
		while (k-- && !nums.empty())		//k--时,有一个判断,即减了3次后就不再执行了。
			q.push(nums.back()), nums.pop_back();		//把nums的末尾元素 接到 q的队列末端,且删除nums的末尾元素
		
		cout << "这是k--跳出后" << endl;
		for (int i = 0; i <= k; i++)		//这段代码不知道是用来防止什么的,可能是用来防止k的值的吧,在这个程序中没有用
			q.push(q.front()), q.pop();		//把q最早被压入队列的元素 接到 q的队列末端,且弹出q队列的第一个元素
		



		print_queue(q);
		while (!q.empty())		//如果q不是空队列,只有一次操作
			nums.insert(nums.begin(), q.front()), q.pop();		//在 nums 开始的地方插入q的 queue.front(),即最早被压入队列的元素  然后q.pop(),删除最早插入的元素
		return;		//一般情况下,返回类型是 void 的函数使用 return 语句是为了引起函数的强制结束。
	}
};




//************************************************************
//  完整程序
//************************************************************

///// Source : https://leetcode.com/problems/rotate-array/description/
///// Author : liuyubobobo
///// Time   : 2018-08-10
//
//#include <iostream>
//#include <vector>
//#include <queue>
//
//using namespace std;
//
//
//
//
//
///// Using Queue
///// Time Complexity: O(n^2)
///// Space Complexity: O(n)
//
//
//
//void print_queue(queue<int>& q1) {
//	queue<int> q2 = q1;
//int n = q2.size();
//int e_cout_for_queue;
//for (int j = 0; j < n; j++)
//{
//	e_cout_for_queue = q2.front();
//	q2.pop();
//	cout << e_cout_for_queue << " ";
//}
//
//}
//
//
//
//
//
////打印vector,不需要理解,会用就行
//void print_vec(const vector<int>& vec) {
//	for (int e : vec) //逐个把vec中的值赋给e,虽然不懂,但感觉好牛的样子
//		cout << e << " ";
//	cout << endl;
//}
//
//
//
////queue.front(),即最早被压入队列的元素。
////queue.back(),即最后被压入队列的元素。
////queue.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
////queue.push(x); 将x 接到队列的末端。
//
////末尾删除元素: vec.pop_back();
////任意位置插入元素: vec.insert();
//class Solution {
//public:
//	void rotate(vector<int>& nums, int k) {
//
//		queue<int> q;
//		while (k-- && !nums.empty())		//k--时,有一个判断,即减了3次后就不再执行了。
//			q.push(nums.back()), nums.pop_back();		//把nums的末尾元素 接到 q的队列末端,且删除nums的末尾元素
//		
//		cout << "这是k--跳出后" << endl;
//		for (int i = 0; i <= k; i++)		//这段代码不知道是用来防止什么的,可能是用来防止k的值的吧,在这个程序中没有用
//			q.push(q.front()), q.pop();		//把q最早被压入队列的元素 接到 q的队列末端,且弹出q队列的第一个元素
//		
//
//
//
//		print_queue(q);
//		while (!q.empty())		//如果q不是空队列,只有一次操作
//			nums.insert(nums.begin(), q.front()), q.pop();		//在 nums 开始的地方插入q的 queue.front(),即最早被压入队列的元素  然后q.pop(),删除最早插入的元素
//		return;		//一般情况下,返回类型是 void 的函数使用 return 语句是为了引起函数的强制结束。
//	}
//};
//
//
//
//
//
//int main() {
//
//	vector<int> nums1 = { 1, 2, 3, 4, 5, 6, 7 };
//	Solution().rotate(nums1, 3);
//	print_vec(nums1);
//
//	vector<int> nums2 = { -1,-100,3,99 };
//	Solution().rotate(nums2, 2);
//	print_vec(nums2);
//
//
//	// while 循环里,如果需要执行多行语句,有两种方式:第一种是多行情况,用 花括号和分号  第二种是用单行,句与句之间用逗号
//	//int i_wanglei = 0;
//	//while (1)		//如果q不是空队列,只有一次操作
//	//	i_wanglei++, cout << i_wanglei << endl, i_wanglei++, cout << i_wanglei << endl;
//	system("pause");
//	return 0;
//}




//////////与主程序无关
//////////以下程序  是关于queue的使用,push后也是推入第一个
////////#include <cstdlib>
////////#include <iostream>
////////#include <queue>
////////using namespace std;
////////int main()
////////{
////////	int e, n, m;
////////	queue<int> q1;
////////	for (int i = 0; i < 10; i++)
////////		q1.push(i);
////////	if (!q1.empty())
////////		cout << "dui lie  bu kongn";
////////	n = q1.size();
////////	cout << n << endl;
////////	m = q1.back();
////////	cout << m << endl;
////////	for (int j = 0; j < n; j++)
////////	{
////////		e = q1.front();
////////		cout << e << " ";
////////		q1.pop();
////////	}
////////	cout << endl;
////////	if (q1.empty())
////////		cout << "dui lie  bu kongn";
////////	system("PAUSE");
////////	return 0;
////////}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值