leetcode_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 ]
]

思路:

这题看起来是很复杂,做起来也确实挺复杂的。但是呢,这题并不是非常非常难,只是控制逻辑让人很抓狂罢了。

colStart,colEnd,rowStart,rowEnd,num=0

1.colStart<colEnd  为arr[row][colStart]~arr[row][colEnd-1]赋值num++;

2.rowStart<rowEnd 为arr[col][rowStart]~ar[col][rowEnd-1]赋值num++;

3.colEnd>colStart 为arr[rowLen-1-row][colEnd]~arr[rowLen-1-row][colStart+1]赋值num++;

4.rowEnd>rowStart 为arr[colLen-1-col][rowEnd]~arr[colLen-1-col][rowStart+1]赋值num++;

代码:

public int[][] generateMatrix(int n) {
		int matrix[][]=null;
		if(n<0)
	        	return matrix;
		else if(n==0)
		{
			matrix=new int [1][];
			matrix[0]=new int[]{};
		}
		matrix=new int[n][];
		for(int i=0;i<n;i++)
			matrix[i]=new int[n];
        int startNum=1;
        int rows=n,cols=n;
        int x=0,y=0;
        while(x<cols-x&&y<rows-y)
        {
        	int end=cols-x-1;
        	if(x<end)
        	{
        		for(int i=x;i<end;i++)
            		matrix[y][i]=startNum++;
        	}

        	end=rows-y-1;
        	if(y<end)
        	{
        		for(int j=y;j<end;j++)
            		matrix[j][cols-x-1]=startNum++;;
        	}
        	int start=cols-x-1;
        	if(start>x)
        	{
        		for(int i=start;i>x;i--)
            		matrix[rows-y-1][i]=startNum++;;
        	}
        	start=rows-y-1;
        	if(start>y)
        	{
        		for(int j=start;j>y;j--)
            		matrix[j][x]=startNum++;
        	}
        	if(2*x+1==cols&&2*y+1==rows)
        		matrix[y][x]=startNum++;;
        	x++;
        	y++;
        }
        return matrix;
    }
时间: 2024-10-08 19:36:40

leetcode_Spiral Matrix II的相关文章

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 Search a 2D Matrix II

题目连接 https://leetcode.com/problems/search-a-2d-matrix-ii/ Search a 2D Matrix II Description Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascend

【LeetCode】240. Search a 2D Matrix II

Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascendin

[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 ] ] [分析] 模拟 [代码] /**-------------------------

每日算法之四十一: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 ] ] 针对这个问题采用最直观的方式即可,即螺旋插入,这里有两个地方需要注意,一个是插入边界的界定,

【LeetCode】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类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ

leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?

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 II

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 ] ] 这道题接着上面那道题,是一个逆过程,只要稍微改一下就好了,因

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]. 思路: 可参考剑指