54螺旋矩阵

54螺旋矩阵,第1张

54螺旋矩阵 54螺旋矩阵 题目描述

题目链接

模拟过程求解
#pragma once
#include 
#include
using namespace std;

class Solution 
{
public:
	vector spiralOrder(vector>& matrix) 
	{
		//创建一个容器用于存放结果
		vectorans;
		//如果为空直接返回空
		if (matrix.empty())
		{
			return ans;
		}

		int row = matrix.size();//行数
		int col = matrix[0].size();//列数
		//元素个数
		int length = row * col;
		//上下左右的初始值
		int up = 0; 
		int down = row - 1;
		int left = 0;
		int right = col - 1;
		//添加元素的个数
		int elemNum = 0;

		while (elemNum < length) 
		{
			//从左到右 遍历列
			for (int j = left; j <= right; j++) 
			{
				ans.push_back(matrix[up][j]);
				elemNum++;
			}
			up++;//上+1
			if (elemNum == length) 
			{
				break;
			}
			//从上到下 遍历行

			for (int i = up; i <= down; i++)
			{
				ans.push_back(matrix[i][right]);
				elemNum++;
			}
			right--;//右-1
			if (elemNum == length)
			{
				break;
			}
			//从右到左 遍历列
			for (int j = right; j >= left; j--) 
			{
				ans.push_back(matrix[down][j]);
				elemNum++;
			}
			down--;//下-1
			if (elemNum == length) 
			{
				break;
			}
			//从下到上 遍历行
			for (int i = down; i >= up; i--) 
			{
				ans.push_back(matrix[i][left]);
				elemNum++;
			}
			left++;//左+1
			if (elemNum == length)
			{
				break;
			}
		}
		return ans;
	}
};

int main()
{
	//创建vector容器嵌套容器
	vector> matrix;
	vectorv1;
	vectorv2;
	vectorv3;

	for (int i = 0; i < 3; i++)
	{
		v1.push_back(i + 1);
		v2.push_back(i + 4);
		v3.push_back(i + 7);
	}

	matrix.push_back(v1);
	matrix.push_back(v2);
	matrix.push_back(v3);


	for (vector>::iterator it = matrix.begin(); it != matrix.end(); it++)
	{
		for (vector::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
		{
			cout << *vit << " ";
		}
		cout << endl;
	}


	Solution S;
	vector res = S.spiralOrder(matrix);

	for (vector::iterator it = res.begin(); it != res.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	system("pause");
	return 0;
}

运行结果

提交结果

官方解答

官方解答

官方方法二:按层模拟 思路

图示

//官方方法二:按层模拟
class Solution1 
{
public:
	vector spiralOrder(vector>& matrix) 
	{
		if (matrix.size() == 0 || matrix[0].size() == 0)
		{
			return {};
		}

		int rows = matrix.size(), columns = matrix[0].size();
		vector order;
		int left = 0, right = columns - 1, top = 0, bottom = rows - 1;
		while (left <= right && top <= bottom) 
		{
			//从左到右遍历上侧元素 遍历的是行
			for (int column = left; column <= right; column++) 
			{
				order.push_back(matrix[top][column]);
			}
			//从上到下遍历右侧元素 遍历的是列
			for (int row = top + 1; row <= bottom; row++)
			{
				order.push_back(matrix[row][right]);
			}

			if (left < right&& top < bottom) 
			{
				//从右到左遍历下侧元素
				for (int column = right - 1; column > left; column--)
				{
					order.push_back(matrix[bottom][column]);
				}
				//下到上遍历左侧元素
				for (int row = bottom; row > top; row--)
				{
					order.push_back(matrix[row][left]);
				}
			}
			

		
			left++;
			right--;
			top++;
			bottom--;
		}
		return order;
	}
};

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5155542.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存