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,中游


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

LeetCode - 54. Spiral Matrix

这道题目也需要寻找矩阵中下标的一定规律,题目中给出的例子是n = 3, m = 3的情况,所以可以先下出来下标的变化情况然后寻找一定的规律。 在逐渐寻找规律的时候,可以发现每次都是将外面一层的元素放...

【LeetCode】54. Spiral Matrix

问题描述https://leetcode.com/problems/spiral-matrix/#/description Given a matrix of m x n elements (m r...

[leetcode] 54.Spiral Matrix

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

Leetcode-54.Spiral Matrix

Problem Description: Given a matrix of m x n elements (m rows, n columns), return all elements of t...

leetcode54 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/59. Spiral Matrix i, ii

1. 题目描述54 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...

LeetCode-54. Spiral Matrix (JAVA)(顺时针打印矩阵)

LeetCode-54. Spiral Matrix (JAVA)(顺时针打印矩阵)

leetcode 54. Spiral Matrix

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

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...

54. Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or...
  • x_shuck
  • x_shuck
  • 2016年06月27日 22:17
  • 81
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Algorithms—54.Spiral Matrix
举报原因:
原因补充:

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