给定一个包含 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. CSS - 视觉格式化模型(Visual formatting model)

    几个概念 块:block,一个抽象的概念,块与块之间在垂直方向上按照顺序依次堆叠. 行内:inline,一个抽象的概念,行内与行内之间在水平方向上按照顺序依次堆叠(会有换行). 元素:element, ...

  2. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_7_HashMap存储自定义类型键值

    自定义类型做key值.必须要重写hashCode和equals方法 创建pserson类 有name个age两个成员变量.重写toString方法 key有重复,会被新的value值替换掉. key值 ...

  3. Jmeter之Switch Controller

    在测试过程中,各种不同的情况需要执行不同的操作,这个时候用if控制器比较麻烦,此时就可以使用Switch Controller代替. 一.界面显示 二.配置说明 1.名称:标识 2.注释:备注 3.S ...

  4. tensorflow学习笔记二:入门基础 好教程 可用

    http://www.cnblogs.com/denny402/p/5852083.html tensorflow学习笔记二:入门基础   TensorFlow用张量这种数据结构来表示所有的数据.用一 ...

  5. 《Python Data Structures》Week5 Dictionary 课堂笔记

    Coursera课程<Python Data Structures> 密歇根大学 Charles Severance Week5 Dictionary 9.1 Dictionaries 字 ...

  6. Learn Python the hard way, ex42 物以类聚

    依然少打很多剧情,并修改了很多,还好,能运行 #!urs/bin/python #coding:utf-8 from sys import exit from random import randin ...

  7. JMeter性能测试入门-不同类型线程组的使用

    jmeter不同线程组的详解 在做性能测试之前,我们来了解一下JMeter多个不同线程组的应用.首先,JMeter提供了三个基本的线程组,分别为: Thread Group setUp Thread ...

  8. Generative Model vs Discriminative Model

    In this post, we are going to compare the two types of machine learning models-generative model and ...

  9. [Git] 002 初识 Git 与 GitHub 之加入文件 第一弹

    在 GitHub 的 UI 界面使用 Git 往仓库里加文件 第一弹 1. 点击右上方的 Create new file 2. 在左上方填入文件名,若有后缀,记得加上 3. 页面跳转,此时已有两个文件 ...

  10. org.springframework.beans.BeanUtils的用法

    s,t为对象BeanUtils.copyProperties(s, t);  将给定源bean的属性值复制到目标bean中 public static void copyProperties(Obje ...