Spiral Matrix 1&2

1.

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

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

package leetcode2;

import java.util.*;

public class SpiralMatrix {
     public static ArrayList<Integer> spiralOrder(int[][] matrix) {
         ArrayList<Integer> res=new ArrayList<Integer>();
           if(matrix==null||matrix.length==0){
               return res;
           }
           int m=matrix.length;
           int n=matrix[0].length;
           int x=0;
           int y=0;
           while(n>0 && m>0){
             if(m==1){
               for(int i=0;i<n;i++){
                   res.add(matrix[x][y++]);
               }
               break;
             }else  if(n==1){
                 for(int i=0;i<m;i++){
                       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++;
             n=n-2;
             m=m-2;

           }
           return res;
        }
     /*
      * SpiralMatrix2
      *   public int[][] generateMatrix(int n) {
        int[][] res=new int[n][n];
        int top=0;
        int left=0;
        int botton=n-1;
        int right=n-1;
        int k=1;
        while(top<botton&&left<right){
            for(int i=left;i<right;i++){
                res[top][i]=k++;
            }
            for(int i=top;i<botton;i++){
                res[i][right]=k++;
            }
            for(int i=right;i>left;i--){
                res[botton][i]=k++;
            }
            for(int i=botton;i>top;i--){
                res[i][left]=k++;
            }
            left++;
            right--;
            top++;
            botton--;
        }
        if(n%2!=0){
            res[n/2][n/2]=k;
        }
        return res;
    }
      */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}
时间: 2024-08-01 08:11:32

Spiral Matrix 1&2的相关文章

Spiral Matrix(LintCode)

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 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]. 难得的一次AC! 虽然感觉题

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][Java] 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 ] ] 题意: 给定一个整数 n,生成一个正方形矩阵.矩阵中包含着从1到n2 这些元素,并且

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

【leetcode】Spiral Matrix

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 Spiral Matrix II

题目:是Spiral Matrix相关的的.这题的意思是给定一个n,那么在n*n的矩阵里按照循环记录将1,2,3,..., n*n.如下如果给定3,那么: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]一开始我想是不是有数学公式直接下标对应的,那直接遍历输出就可以了.但是推了一会,没有什么好的头绪.于是就分情况讨论了,和Spiral Matrix一样的分情况,只是这里的分情况是记录在数组里面.利用上,下,左,右,分别记录每次循环可以到达的四个边界,我们每次就输

每日算法之四十一: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 54

54.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]. 好久没做题了,