矩阵螺旋遍历Spiral Matrix,Spiral Matrix2

SpiralMatrix:

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example,
Given the following matrix:

[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]

You should return [1,2,3,6,9,8,7,4,5].

算法分析:其实就是两重循环遍历,每次都是遍历一圈,然后遍历里面一圈...,需要记录m,n,每次遍历后,m,n都减去2;还要记录坐标x,y;

特例是n=1;m=1;其实m,n为奇数才有这种特例。

public class SpiralMatrix
{
	public List<Integer> spiralOrder(int[][] matrix)
	{
		List<Integer> res = new ArrayList<>();
		if(matrix.length == 0 || matrix == null)
		{
			return res;
		}
		int m = matrix.length;
		int n = matrix[0].length;//得先判断矩阵是否为空,否则数组下标越界
		int x = 0, y = 0;
		while(m > 0 && n > 0)
		{
			if(m == 1)
			{
				for(int i = 0; i < n; i ++)
				{
					res.add(matrix[x][y++]);
				}
				break;//别忘了break
			}
			else if(n == 1)
			{
				for(int j = 0; j < m; j ++)
				{
					res.add(matrix[x++][y]);
				}
				break;
			}

			for(int i = 0; i < n-1; i ++)
			{
				res.add(matrix[x][y++]);
			}
			for(int i = 0; i < m-1; i ++)
			{
				res.add(matrix[x++][y]);
			}
			for(int i = 0; i < n-1; i ++)
			{
				res.add(matrix[x][y--]);
			}
			for(int i = 0; i < m-1; i ++)
			{
				res.add(matrix[x--][y]);
			}
			x++;
			y++;
			m -= 2;
			n -= 2;
		}
		return res;
	}
}

SpiralMatrix2:

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

For example,
Given n = 3,

You should return the following matrix:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

算法分析:和上一算法,类似,只不过这是个n*n矩阵,其实可以延伸到m*n矩阵。n*n使清空更简单,特别是n=1的时候。

public class SpiralMatrix2
{
	public int[][] generateMatrix(int n)
	{
		int[][] res = new int[n][n];
		if(n == 0)
		{
			return res;
		}
		int x = 0, y = 0;
		int val = 1;
		while(n > 0)
		{
			if(n == 1)
			{
				res[x][y] = val;
				break;
			}
			for(int i = 0; i < n-1; i ++)
			{
				res[x][y++] = val;
				val ++;
			}
			for(int i = 0; i < n-1; i ++)
			{
				res[x++][y] = val;
				val ++;
			}
			for(int i = 0; i < n-1; i ++)
			{
				res[x][y--] = val;
				val ++;
			}
			for(int i = 0; i < n-1; i ++)
			{
				res[x--][y] = val;
				val ++;
			}
			x ++;
			y ++;
			n -= 2;
		}
		return res;
	}
}
时间: 2024-10-08 07:54:20

矩阵螺旋遍历Spiral Matrix,Spiral Matrix2的相关文章

Spiral Matrix &amp;&amp; Spiral Matrix II

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 解题思路: 求一个

59. Spiral Matrix &amp;&amp; Spiral Matrix II

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 思路: 可参考剑指

LeetCode 885. Spiral Matrix III

原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题目标签:Array 这道题目和之前的螺旋矩阵几乎没有区别,而且更简单.同样按照螺旋矩阵的特性

[C++]LeetCode: 110 Spiral Matrix II (螺旋写入矩阵)

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 思路:这道题和Spiral Matrix的模型和解法一样.要求我们将1~n^2的数字

Leetcode 54:Spiral Matrix 螺旋矩阵

54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1

每日算法之四十一:Spiral Matrix II (螺旋矩阵)

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 针对这个问题采用最直观的方式即可,即螺旋插入,这里有两个地方需要注意,一个是插入边界的界定,