54. Spiral Matrix java solutions

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

Subscribe to see which companies asked this question

 1 public class Solution {
 2     public List<Integer> spiralOrder(int[][] matrix) {
 3         List<Integer> ans = new ArrayList<Integer>();
 4         if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return ans;
 5         int startx = 0,starty = 0,endx = matrix[0].length-1,endy = matrix.length-1;
 6
 7         while(startx <= endx && starty <= endy){
 8             for(int i = startx;i<=endx && starty <= endy;i++){// 该题这里需要判断下 starty endy 的边界,不然容易重复加入list
 9                 ans.add(matrix[starty][i]);
10             }
11             starty++;
12
13             for(int i = starty;i<=endy && startx <= endx;i++){
14                  ans.add(matrix[i][endx]);
15             }
16             endx--;
17
18             for(int i = endx;i>=startx && starty <= endy;i--){
19                 ans.add(matrix[endy][i]);
20             }
21             endy--;
22
23             for(int i = endy;i>=starty && startx <= endx;i--){
24                 ans.add(matrix[i][startx]);
25             }
26             startx++;
27         }
28         return ans;
29     }
30 }

对比59

Spiral Matrix II java solutions

http://www.cnblogs.com/guoguolan/p/5620000.html

时间: 2024-11-11 13:42:46

54. Spiral Matrix java solutions的相关文章

leetcode 54 Spiral Matrix ----- java

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 54:Spiral Matrix 螺旋矩阵

54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1

[LeetCode]54. Spiral Matrix

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]. 题意: 根据给

Leetcode 54. Spiral Matrix &amp; 59. Spiral Matrix II

54. Spiral Matrix [Medium] Description 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:

[Leetcode][Python]54: Spiral Matrix

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 54: Spiral Matrixhttps://leetcode.com/problems/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 foll

leetCode 54.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]54. Spiral Matrix二维数组螺旋取数

import java.util.ArrayList; import java.util.List; /** * 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

[leedcode 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]. public class Solution {

54. Spiral Matrix

题目: Given a matrix of m x n elements (m rows, ncolumns), 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]. 链接: 题解: 测试: