新博文地址:[leetcode]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 ]
]
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
这道题是《剑指offer》上的例题,上面的解法很简单,但是我没用,我的算法更简单,而且也是O(m*n)的复杂度,但是需要O(m*n)的空间,当然,也可以不开额外的空间,直接修改原数组。
算法太简单了:
主要就是维护一个visit数组,来标记这个元素是否已经访问过,每圈都是从(0,0)(1,1)这样的(n,n)点开始扫描的,因此startLine表示扫描轮数,startLine <= min(rowCount,columnCount) / 2
不罗嗦了,直接看代码吧。
public List<Integer> spiralOrder(int[][] matrix) { List<Integer> list = new ArrayList<Integer>(); int height = matrix.length; if(height == 0) return list; int width = matrix[0].length; if(width == 1){ for(int i = 0 ; i < height; list.add(matrix[i][0]),i++); return list; } boolean[][] visited = new boolean[height][width]; int startLine = 0; for(; startLine <= Math.min(height, width) / 2; startLine++){ for(int i = 0; i < width; i++){ if(!visited[startLine][i]){ addToList(matrix, startLine, i, list, visited); } } for(int i = 0 ; i < height; i++){ if(!visited[i][width - 1 - startLine]){ addToList(matrix, i,width - 1- startLine, list, visited); } } for(int i = width - 1; i >= 0; i--){ if(!visited[height - 1 - startLine][i]){ addToList(matrix, height - 1 - startLine, i, list, visited); } } for(int i = height - 1; i >= 0 ; i--){ if(!visited[i][startLine]){ addToList(matrix, i,startLine, list, visited); } } } return list; } private void addToList(int[][] matrix,int row,int column,List<Integer> list,boolean visited[][]){ list.add(matrix[row][column]); visited[row][column] = true; }
相关推荐
# [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...
leetcode 答案螺旋矩阵 返回二维数组中整数元素的顺时针螺旋列表 [答案击败 100% Java LeetCode 运行时提交] [答案击败 100% Java LeetCode 内存使用提交] 大(O)= O(N)
leetcode category other hot keywords:Palindrome(mic), Subsequence Array 螺旋矩阵Spiral Matrix 顺时针打印矩阵 Next Permutation Product of Array Except Self 189.rotate-array 283.move-zero Range Sum ...
Spiral Matrix com.leetcode.list Linked List Cycle Linked List Cycle II Remove Duplicates from Sorted List com.leetcode.string Single Number com.leetcode.tree Balanced Binary Tree Maximum Depth of ...
多线程 leetcode 前言 ...Spiral Matrix Path Sum II Copy List with Random Pointer Building H2O Fizz Buzz Multithreaded hard Merge k Sorted Lists Reverse Nodes in k-Group Trapping Rain Water
第 338 章leetcode_cpp leetcode 的 C++ 代码 包含的问题 1 两和容易2 加两个数中5 最长回文子串中6 ZigZag 转换介质7 反转整数简单8 ...Spiral Matrix II 培养基61 轮播列表中第62话第63话64 最小路径和中66
Spiral Matrix medium O 66 Plus One easy O O 118 Pascal's Triangle easy O O 119 Pascal's Triangle II easy O 要满足只用一个array大小空间O(k) k为input大小来完成,须具备backtracking概念 151 Reverse Words ...
leetcode LeetCode Solutions 算法 - Algorithms 排序算法:快速排序、归并排序、计数排序 搜索算法:回溯、递归、剪枝技巧 图论:最短路、最小生成树、网络流建模 动态规划:背包问题、最长子序列、计数问题 基础...
leetcode 530 力码 全部的: 易(173/237+x) 中(144/437+x) 硬(4/x) 问题 1.Two Sum(dict) 7.(跳过)(数学) 9.(跳过)(串串技巧) 11.盛水最多的容器 12.(跳过)(问题不好) 13.(跳过)(蛮力) 14.(跳过)...
java lru leetcode LeetCode 记录数据结构与算法/LeetCode练习过程,将...Spiral Matrix Mergesort [Algorithm Swap](Mergesort/Algorithm swap.md) Numbers Queue Stack Toposort Trie Tree Two Pointers Union Find
对于每一道算法题会总结代码、时间复杂度以及一些好的blog排序(sort)...Spiral Matrix IILeetCode 53 Maximum SubarrayLeetCode 152 Maximum Product SubarrayLintCode 138 Subarray SumLintCode 139 Subarray Sum ...
这是我准备面试时建议的个人问题清单。...https://leetcode.com/problems/spiral-matrix/ https://leetcode.com/problems/search-a-2d-matrix/ https://leetcode.com/problems/set-matrix-zeroes/ 弦乐 ...
Spiral Matrix II ZigZag Conversion Divide Two Integers Text Justification Max Points on a Line Community QQ 群: 237669375 Github: https://www.github.com/soulmachine/algorithm-essentials 微博: @...
def spiralOrder(self, matrix: List[List[int]]) -> List[int]: if not matrix:return [] m,n = len(matrix),len(matrix[0]) x = y = di = 0 dx = [0,1,0,-1] dy = [1,0,-1,0] res = [] visite