[leetcode]54. Spiral Matrix螺旋矩阵
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:
- [
- [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]
题意:
按螺旋方式遍历矩阵。
Solution1: Simulation the process and implement it.
code
- class Solution {
- public List<Integer> spiralOrder(int[][] matrix) {
- List<Integer> res = new ArrayList<>();
- if(matrix.length == 0 || matrix[0].length == 0) return res;
- int top = 0;
- int bottom = matrix.length-1;
- int left = 0;
- int right = matrix[0].length-1;
- while(true){
- for(int i = left; i <= right; i++) {
- res.add(matrix[top][i]);
- }
- top++;
- if(left > right || top > bottom) break;
- for(int i = top; i <= bottom; i++) {
- res.add(matrix[i][right]);
- }
- right--;
- if(left > right || top > bottom) break;
- for(int i = right; i >= left; i--) {
- res.add(matrix[bottom][i]);
- }
- bottom--;
- if(left > right || top > bottom) break;
- for(int i = bottom; i >= top; i--){
- res.add(matrix[i][left]);
- }
- left++;
- if(left > right || top > bottom) break;
- }
- return res;
- }
- }
[leetcode]54. Spiral Matrix螺旋矩阵的更多相关文章
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 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 ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- PAT甲级——1105 Spiral Matrix (螺旋矩阵)
此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...
随机推荐
- MySQL Error--The Table is full
问题描述 在MySQL 错误日志中发下以下错误信息:[ERROR] /export/servers/mysql/bin/mysqld: The table '#sql-xxxx-xxx' is ful ...
- 算法笔记 3.2 codeup1934 找X
#include <stdio.h> ; int a[maxn]; int main(void){ int n; while(scanf("%d", &n)!= ...
- SQL 查询当天,本月,本周的记录
SELECT * FROM 表 WHERE CONVERT(Nvarchar, dateandtime, 111) = CONVERT(Nvarchar, GETDATE(), 111) ORDE ...
- forward reference前向引用,gloal values and local values全局变量和局部变量,recursive function递归函数
1.全局变量与局部变量练习 1 # -*- coding: UTF-8 -*- 2 def bar(): 3 print('from bar') 4 def foo(): 5 print('from ...
- 黄聪:如何扩展Chrome DevTools来获取页面请求
1. Chrome DevTools Extension 熟悉React的同学,可能对React Developer Tools并不陌生, 刚看到的时候,我也觉得很神奇, 因为React De ...
- Git-撤销(回退)已经add,commit或push的提交
本文只阐述如何解决问题,不会对git的各种概念多做介绍,如果有兴趣可以点击下面的链接,进行详细的学习:Pro Git本文适用的环境 现在先假设几个环境,本文将会给出相应的解决方法:1. 本地代码(或文 ...
- 2017-2018-2 20165312实验二《Java面向对象程序设计》实验报告
2017-2018-2 20165312实验二<Java面向对象程序设计>实验报告 实验中遇到的问题 1.增加MyUtil的测试类之后,TestCase是红色的,但是没有找到junit.j ...
- vim中行末去掉^M
方式1: 输入 :%s/^M//g 方式2: 输入:%s/\r//g
- nginx的proxy_pass路径转发规则最后带/问题
一.location匹配路径末尾没有 / location /sta{proxy_pass http://192.168.1.1/sta;} 外面访问:http://外网IP/sta/sta1.htm ...
- JOSN转列格式(csv文件)
推荐网站 https://json-csv.com/ 限制1M大小