算法----Spiral Matrix

Spiral Matrix I

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

Example 1:Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5]

Example 2:Input: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] Output: [1,2,3,4,8,12,11,10,9,5,6,7]

螺旋打印二维数组

Intuition

The answer will be all the elements in clockwise order from the first-outer layer, followed by the elements from the second-outer layer, and so on.

Algorithm

We define the k-th outer layer of a matrix as all elements that have minimum distance to some border equal to k.

For example, the following matrix has all elements in the first-outer layer equal to 1, all elements in the second-outer layer equal to 2, and all elements in the third-outer layer equal to 3.

[[1, 1, 1, 1, 1, 1, 1],
 [1, 2, 2, 2, 2, 2, 1],
 [1, 2, 3, 3, 3, 2, 1],
 [1, 2, 2, 2, 2, 2, 1],
 [1, 1, 1, 1, 1, 1, 1]]

假定左上角坐标为r1,c1  右下角坐标为r2,c2,从左上角开始顺时针打印二维数组

For each outer layer, we want to iterate through its elements in clockwise order starting from the top left corner. Suppose the current outer layer has top-left coordinates (r1, c1) and bottom-right coordinates (r2, c2).

Then, the top row is the set of elements (r1, c) forc = c1,...,c2, in that order.

The rest of the right side is the set of elements (r, c2) for r = r1+1,...,r2, in that order.

Then, if there are four sides to this layer (ie., {c1 < c2}c1 < c2), we iterate through the bottom side and left side as shown in the solutions below.

 

代码如下:

从上右下左顺序依次打印,上面从c1到c2,右边从r1+1到r2,下边从c2+1到c1+1,左边从r2到r1+1

注意上下左右打印的范围,以及下和左进行打印的条件

public List<Integer> spiralOrder2(int[][] matrix) {
        List<Integer> list = new ArrayList<>();
        if(matrix.length==0) return list;
        int r1 = 0;
        int r2 = matrix.length-1;
        int c1 = 0;
        int c2 = matrix[0].length-1;
        
        while(r1<=r2 && c1<=c2){
        	
        	for(int c=c1; c<=c2; c++) list.add(matrix[r1][c]);
        	
        	for(int r=r1+1; r<=r2; r++) list.add(matrix[r][c2]);
        	
        	if(r1<r2 && c1<c2){
        		
        		for(int c=c2-1; c>c1; c--) list.add(matrix[r2][c]);
        		
        		for(int r=r2; r>r1; r--) list.add(matrix[r][c1]);
        	}
        	r1++; r2--; c1++; c2--;
 
        }
        return list;
    }

Spiral Matrix II

Given a positive integer n, generate a square matrix filled with elements from 1 to n2in spiral order.

Example:Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]

public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
		int r1 = 0;
		int r2 = n-1;
		int c1 = 0;
		int c2 = n-1;
		int k = 1;
		while(r1<=r2 && c1<=c2){
			for(int c=c1; c<=c2; c++) result[r1][c] = k++;
			for(int r=r1+1; r<=r2; r++) result[r][c2] = k++;
			if(r1<r2 && c1<c2){
				for(int c=c2-1; c>c1; c--) result[r2][c] = k++;
				for(int r=r2; r>r1; r--) result[r][c1] = k++;
			}
			r1++;c2--;r2--;c1++;
		}
		return result;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值