LeetCode--054--区螺旋矩阵(java)
给定一个包含 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)的更多相关文章
- 螺旋矩阵 java实现(待消化)
import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/4 17:13 * @description ...
- 【LeetCode】59.螺旋矩阵II
59.螺旋矩阵II 知识点:数组: 题目描述 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . 示例 输入:n = 3 ...
- 【LeetCode】54. 螺旋矩阵
54. 螺旋矩阵 知识点:数组: 题目描述 给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素. 示例 输入:matrix = [[1,2,3],[4,5, ...
- [LeetCode] 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 spir ...
- Java实现 LeetCode 59 螺旋矩阵 II
59. 螺旋矩阵 II 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ...
- Java实现 LeetCode 54 螺旋矩阵
54. 螺旋矩阵 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], ...
- LeetCode 59. Spiral Matrix II (螺旋矩阵之二)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 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 ...
随机推荐
- (转)SQLite部署-无法加载 DLL“SQLite.Interop.dll”: 找不到指定的模块
本文转载自:http://www.cnblogs.com/muzhiye/p/4284070.html 近期刚使用SQLite,主要引用的是System.Data.SQLite.dll这个dll,在部 ...
- FOUC(Flash Of Unstyled Content)文档样式闪烁
今天看面试题看到了这个新名词..我以前是没有发现过这种状况,应该是我一直都是将加载 CSS 的 link 标签写到 head 里的缘故吧. 什么是文档样式闪烁(Flash Of Unstyled Co ...
- JSON基础,简单介绍
JSON(JavaScript Object Notation(记号.标记)) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - ...
- SQL*Plus 与数据库的交互
设置SQL *Plus的运行环境 SET 命令格式: set system_variable value pagesize :从顶部标题到页结束之间的行数 默认是14 newpage:一页中空行的数量 ...
- 类Random
/* * Random:产生随机数的类 * * 构造方法 * public Random();没有给种子,用的是默认种子,是当前时间的毫秒值 * public Random(long seed);使用 ...
- c# 解决Socket问题——由于目标机器积极拒绝,无法连接
关于单机出现这种问题不多赘述,主要阐述服务机和客户机出现这种问题的解决办法. 1.检查防火墙 这种问题出现的最多,特别是你的服务机还是买的各家的云产品,比如阿里云就是到防火墙中添加出入站规则,Azur ...
- 【ABAP系列】SAP ABAP 仓库库存-物料拆分的算法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 仓库库存-物料 ...
- IQueryable在LINQ中
IQueryable接口定义如下: // 摘要: // 提供对未指定数据类型的特定数据源的查询进行计算的功能. public interface IQueryable : IEnumerable { ...
- [Python3] 013 集合:你不能两次进入同一个集合
目录 0. 集合的独白 1. 集合的创建 2. 集合的特性 (1) 概述 (2) 少废话,上例子 3. 集合的遍历 4. 集合内涵 5. 集合的内置方法 6. 可供集合使用的一些方法/函数 (1) 又 ...
- Jenkins安装配置 远程发布SpringBoot项目
环境要求: Java : 1.8.0_161. Maven :http://maven.apache.org/download.cgi 3.6.1 下载完解压,配置环境变量:vim /etc/prof ...