LeetCode第[54]题(Java):Spiral Matrix

题目:螺旋矩阵

难度:Medium

题目内容

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

我的思路:因为每一圈都是由四条边组成的,所以写一个while死循环,里面有四个for循环,每个for循环结束之后都将相应的指标减少,同时判断是否超标,超标就退出。

eg:

while (true) {

// 横着 for  add

// top ++

// top 是否小于bottom 是则break

。。。

我的代码:

 1     public List<Integer> spiralOrder(int[][] matrix) {
 2         List<Integer> res = new ArrayList<Integer>();
 3         if(matrix.length == 0 || matrix[0].length == 0) return res;
 4
 5         int top = 0;
 6         int bottom = matrix.length-1;
 7         int left = 0;
 8         int right = matrix[0].length-1;
 9
10         while(true){
11             for(int i = left; i <= right; i++) res.add(matrix[top][i]);
12             top++;
13             if(left > right || top > bottom) break;
14
15             for(int i = top; i <= bottom; i++) res.add(matrix[i][right]);
16             right--;
17             if(left > right || top > bottom) break;
18
19             for(int i = right; i >= left; i--) res.add(matrix[bottom][i]);
20             bottom--;
21             if(left > right || top > bottom) break;
22
23             for(int i = bottom; i >= top; i--) res.add(matrix[i][left]);
24             left++;
25             if(left > right || top > bottom) break;
26         }
27
28         return res;
29     }

我的复杂度:O(N * M)     行乘以列

编码过程中的问题

1、没有注意到当输入为空的数组的时候 right 和bottom都会取成负数,所以得加上判空。

答案代码

和我一样的。

原文地址:https://www.cnblogs.com/Xieyang-blog/p/9032743.html

时间: 2024-08-28 12:52:34

LeetCode第[54]题(Java):Spiral Matrix的相关文章

[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第[73]题(Java):Set Matrix Zeroes(矩阵置0)

题目:矩阵置0 难度:Easy 题目内容: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. 翻译: 给定一个m x n矩阵,如果一个元素是0,就把它的整行和列设为0. 要求:就地置0. Example 1: Input: [   [1,1,1],   [1,0,1],   [1,1,1] ] Output: [   [1,0,1],   [0,0,0],  

LeetCode第[26]题(Java):Remove Duplicates from Sorted Array 标签:Array

题目难度:Easy 题目: Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extr

LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 翻译: 给定一个字符串s,找出s中最长的回文子串.你可以假设s的最大长度是1000. 什么叫回文子串? 就是字符串中,满足能正读反读都一样的子串,就是回文子串.如下所示 Input: "babad"

LeetCode第[7]题(Java):Reverse Integer 标签:数学

题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assum

LeetCode第[42]题(Java):Trapping Rain Water

题目:接雨水 难度:hard 题目内容: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In

LeetCode第[48]题(Java):Rotate Image

题目:矩阵旋转 难度:Medium 题目内容: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate anot

LeetCode第[79]题(Java):Word Search(矩阵单词搜索)

题目:矩阵单词搜索 难度:Medium 题目内容: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same

LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is &q