给定一个包含 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] 思路:把每次打印看成是打印当前矩阵的外圈,当前矩阵打印完成后,找到其子矩阵,继续打印子矩阵的外圈。
比如例子1的矩阵 1 2 3
4 6
7 8 9
如何确定一个矩阵,只需要确定其左上角的点和右下角的点,我们把左上角的点即为(tR,tC) 把右下角的点记为(dR,dC)
(0,0) (2,2)即为上面矩阵的两个点,打印一圈后,左上角的点往右下移动一个位置,右下角的点往左上移动一个位置,停止条件为,左上角的点跑到右下角点的右边或者下边。
 class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<>();
if(matrix.length == 0 || matrix == null)return list;
int tR = 0;
int tC = 0;
int dR = matrix.length - 1;
int dC = matrix[0].length - 1;
while (tR <= dR && tC <= dC){
printEdge(matrix,tR++,tC++,dR--,dC--,list);
}
return list;
}
public static void printEdge(int[][] m,int tR,int tC,int dR,int dC,List<Integer> list){
if(tR == dR){
for(int i = tC;i <= dC;i++){
list.add(m[tR][i]);
}
}else if (tC == dC){
for(int i = tR;i <= dR;i++){
list.add(m[i][tC]);
}
}else{
int curC = tC;
int curR = tR;
while(curC != dC){
list.add(m[tR][curC]);
curC++;
}
while (curR != dR){
list.add(m[curR][dC]);
curR++;
}
while(curC != tC){
list.add(m[dR][curC]);
curC--;
}
while(curR != tR){
list.add(m[curR][tC]);
curR--;
}
}
}
}

python:

 class Solution:
def draw(self,tR,tC,dR,dC,matrix,lists):
flag = False
i = tC
j = tR
while(i <= dC):           #向右走
lists.append(matrix[tR][i])
print(i)
i+=1
i-=1
j+=1
while(j <= dR): #向下走 lists.append(matrix[j][dC])
j+=1
j-=1
i-=1
if j == dR and dR != tR: #向左走
while(i >=tC):
lists.append(matrix[dR][i])
i-=1
flag = True
i += 1
j -= 1
if i == tC and flag: #没往左走则不往上走
while(j > tR):
lists.append(matrix[j][tC])
j-=1
j += 1
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if len(matrix) == 0:
return []
if len(matrix[0]) == 1:
return [matrix[i][0] for i in range(len(matrix))] #一列的情况
if len(matrix) == 1:
return [matrix[0][i] for i in range(len(matrix[0]))] #一行的情况
tR = tC = 0
dR = len(matrix) - 1
dC = len(matrix[0]) - 1
lists = []
while dR >= tR and dC >= tC:
self.draw(tR,tC,dR,dC,matrix,lists)
tR += 1
tC += 1
dR -= 1
dC -= 1
return lists

参考:程序员代码面试指南

2019-05-13 15:47:27

2019-08-27 20:08:59

LeetCode--054--区螺旋矩阵(java)的更多相关文章

  1. 螺旋矩阵 java实现(待消化)

    import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/4 17:13 * @description ...

  2. 【LeetCode】59.螺旋矩阵II

    59.螺旋矩阵II 知识点:数组: 题目描述 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . 示例 输入:n = 3 ...

  3. 【LeetCode】54. 螺旋矩阵

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

  4. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  5. [算法][LeetCode]Spiral Matrix——螺旋矩阵

    题目要求 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spir ...

  6. Java实现 LeetCode 59 螺旋矩阵 II

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

  7. Java实现 LeetCode 54 螺旋矩阵

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

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

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

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

随机推荐

  1. p4841 城市规划

    分析 https://www.luogu.org/blog/DRA/solution-p4841 代码(似乎附赠了一个全家桶呢) #pragma GCC optimize(2) #pragma GCC ...

  2. day52—JavaScript拖拽事件的应用(自定义滚动条)

    转行学开发,代码100天——2018-05-07 前面的记录里展示了JavaScript中鼠标拖拽功能,今天利用拖拽功能实现另一个应用场景——自定义滚动条(作为控制器)的用法. 常通过自定义滚动条控制 ...

  3. linux(centos6.5)常用命令

    前言:由于项目项目使用的是linux服务器,因此会使用到较多linux命令,本文对centos下常用命令进行记录 1.vi的三种模式 2.解压缩相关 3.用户相关 4.文件相关 5.各种查看命令 1. ...

  4. mysql 添加外键报错:

    1.报错信息 Cannot add or update a child row: a foreign key constraint fails 2.原因分析 [1]字段的数据类型 父表: 子表: 以上 ...

  5. 如何统计序列中元素的频度---Python数据结构与算法相关问题与解决技巧

    实际案例: 1. 某随机序列 [12,5,6,4,6,5,5,7]中,找到出现次数最高的3个元素,它们出现的次数是多少? 2. 对于某英文文章的单词,进行词频统计,找到出现次数最高的10个单词,它们出 ...

  6. PL/SQL Developer 报错Dynamic Performance Tables not accessible, Automatic Statistics disabled for this session You can disable statistics in the preference menu, or obtain select priviliges on the V$ses

    可以从以下几个方面考虑: 1)单独给用户授动态性能视图的权限: SQL> grant select on V_session  to user; SQL> grant select on  ...

  7. 20190920 On Java8 第二十章 泛型

    第二十章 泛型 多态的泛化机制: 将方法的参数类型设为基类: 方法以接口而不是类作为参数: 使用泛型: 泛型实现了参数化类型 简单泛型 Java 泛型的核心概念:你只需告诉编译器要使用什么类型,剩下的 ...

  8. python基础-7模块,第三方模块安装方法,使用方法。sys.path os sys time datetime hashlib pickle json requests xml

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  9. [19/05/17-星期五] HTML_body标签(内嵌标签)和框架标签

    一.内嵌标签 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <!- ...

  10. CSU-1110 RMQ with Shifts (单点更新+区间最小值 zkw线段树)

    In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query ...