Algorithms—54.Spiral Matrix

原创 2015年07月09日 14:26:26

思路:写了4条读取方法。

public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
    	List<Integer> list=new ArrayList<Integer>();
    	//m:行,n列
    	int mb=0;
    	int nb=0;
        int me=matrix.length;
        if (me==0) {
			return list;
		}
        int ne=matrix[0].length;
    	while (true) {
    		list=new Solution().right(matrix, mb, nb, me, ne, list);
			mb++;
			if (nb>ne-1||mb>me-1) {
				return list;
			}
			list=new Solution().low(matrix, mb, nb, me, ne, list);
			ne--;
			if (nb>ne-1||mb>me-1) {
				return list;
			}
			list=new Solution().left(matrix, mb, nb, me, ne, list);
			me--;
			if (nb>ne-1||mb>me-1) {
				return list;
			}
			list=new Solution().up(matrix, mb, nb, me, ne, list);
			nb++;
			if (nb>ne-1||mb>me-1) {
				return list;
			}
		}
    }
    public List<Integer> right(int[][] matrix,int mb,int nb,int me,int ne,List<Integer> list){
    	for (int i = nb; i < ne; i++) {
			list.add(matrix[mb][i]);
		}
    	return list;
    }
    public List<Integer> left(int[][] matrix,int mb,int nb,int me,int ne,List<Integer> list){
    	for (int i = ne-1; i >=nb; i--) {
			list.add(matrix[me-1][i]);
		}
    	return list;
    }
    public List<Integer> low(int[][] matrix,int mb,int nb,int me,int ne,List<Integer> list){
    	for (int i = mb; i < me; i++) {
			list.add(matrix[i][ne-1]);
		}
    	return list;
    }
    public List<Integer> up(int[][] matrix,int mb,int nb,int me,int ne,List<Integer> list){
    	for (int i = me-1; i >=mb; i--) {
			list.add(matrix[i][nb]);
		}
    	return list;
    }
}


耗时:272ms,中游


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

54. Spiral Matrix (有待进一步研究)

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

【leetcode】【54】Spiral Matrix

一、问题描述 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...

leetcode-java-54. Spiral Matrix

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

(java)leetcode-54:Spiral Matrix

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the ma...

LeetCode#54. Spiral Matrix

按照Spider Order遍历数组:一个二维数组瞬时间绕圈走,将经过的数组元素以List返回

[LeetCode]54.Spiral Matrix

【题目】 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in...

Leetcode 54. Spiral Matrix

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

LeetCode54/59 Spiral Matrix I/II

一:Spiral Matrix I 题目: Given a matrix of m x n elements (m rows, n columns), return all elements ...

[LeetCode]--54. Spiral Matrix

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

leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of th...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)