LeetCode:螺旋矩阵【54】

题目描述

给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

示例 1:

输入:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]

示例 2:

输入:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]

题目分析

  这道题简直丧心病狂☺!我们采用的方式是一圈一圈打印!

  答案将是从第一个外层按顺时针顺序排列的所有元素,然后是第二个外层的元素,依此类推。

  我们首先定义四个元素,r1,r2,c1,c2,这将框定一个范围,我们顺时针打印这个范围边上的值,每次打印以后再次缩小框。

  好的问题来了?

  1.要打印几个框?

    times=Math.min(长,宽)%2==0?Math.min(长,宽)/2:Math.min(长,宽)/2+1;

  2.顺时针打印的横纵坐标变化规律?如图所示有颜色是要打印的框

  

  3、吐槽一下这个题,简直恶心。

Java题解

class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> ans = new ArrayList<>();
int m = matrix.length; //行
if (m == 0)
return ans;
int n = matrix[0].length;//列
int c1 = 0;
int c2 = n-1;
int r1 = 0;
int r2 = m-1;
int times = Math.min(m,n)%2==0?Math.min(m,n)/2:Math.min(m,n)/2+1;
for(int i=0;i<times;i++)
{
for (int c = c1; c <= c2; c++) ans.add(matrix[r1][c]);
for (int r = r1 + 1; r <= r2; r++) ans.add(matrix[r][c2]);
if (r1 < r2 && c1 < c2) {
for (int c = c2 - 1; c > c1; c--) ans.add(matrix[r2][c]);
for (int r = r2; r > r1; r--) ans.add(matrix[r][c1]);
}
r1++;
r2--;
c1++;
c2--;
} return ans;
}
}

  

LeetCode:螺旋矩阵【54】的更多相关文章

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

  2. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  3. Java实现 LeetCode 54 螺旋矩阵

    54. 螺旋矩阵 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], ...

  4. 【LeetCode】54. 螺旋矩阵

    54. 螺旋矩阵 知识点:数组: 题目描述 给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素. 示例 输入:matrix = [[1,2,3],[4,5, ...

  5. leetcode 54. 螺旋矩阵 及 59. 螺旋矩阵 II

    54. 螺旋矩阵 问题描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, ...

  6. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

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

  7. LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

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

  8. LeetCode:螺旋矩阵||【59】

    LeetCode:螺旋矩阵||[59] 题目描述 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ...

  9. 【python】Leetcode每日一题-螺旋矩阵2

    [python]Leetcode每日一题-螺旋矩阵2 [题目描述] 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . ...

随机推荐

  1. mysql数据库记录

    ON DELETE restrict(约束):当在父表(即外键的来源表)中删除对应记录时,首先检查该记录是否有对应外键,如果有则不允许删除. no action:意思同restrict.即如果存在从数 ...

  2. Unix 环境高级编程

    UNIX 环境高级编程 本书描述了UNIX系统的程序设计接口--系统调用接口和标准C库提供的很多函数. 与大多数操作系统一样,Unix为程序员运行提供了大量的服务--打开文件,读文件,启动一个新程序, ...

  3. c++ simple class template example: Stack

    main.cpp #include "Stack.h" #include <iostream> using namespace std; class Box { pub ...

  4. 分布式服务框架 Zookeeper(四)官方编程指南

    握草,是不是加了官方两个字就可以唬人了. 使用ZooKeeper开发分布式应用 简介 这篇文档是为了那些想利用ZooKeeper的协调服务来构建分布式应用的开发人员而写滴,不相干的走一边去哈.在这儿有 ...

  5. 【问题】CentOS6.5系统"libc.so.6: version 'GLIBC_2.15' not found"解决方法

    出现"libc.so.6: version 'GLIBC_2.15' not found"问题,是由于glibc版本过低,升级glibc即可. 由于CentOS系统RPM源目前gl ...

  6. tinyint(4),tinyint(80)有什么区别

    tinyint格式: TINYINT[(M)] [UNSIGNED] [ZEROFILL] M默认为4 Tinyint占用1字节的存储空间,即8位(bit). 带符号的范围是-128到127.无符号的 ...

  7. 初窥ElasticSearch

    初窥ElasticSearch 官网上面的,不知道讲的是什么.. youtube上面有一个start with,内容是在windows以下跑这个elastic search,然后用一个fidler工具 ...

  8. nginx配置后只有根目录首页index.php能访问,其他页面404

    只有首页面根目录可以访问,其他页面地址都是404 not found.网上找了半天url重定向,url重写都试了无效,要不就是重定向过多,下图为跳坑历程. location / { #if ($htt ...

  9. CentOS6.X 升级内核至 3.10

    1.1 查看当前版本 [root@localhost ~]# uname -r -.el6.x86_64 1.2 导入public key [root@localhost ~]# rpm --impo ...

  10. ERROR 1130: Host &#39;&#39; is not allowed to connect to thisMySQL server

    今天1网友求助,说自己PHPmyadmin能够正常连接数据库,使用sqlyog报错: ERROR 1130: Host '172.27.214.1' is not allowed to connect ...