【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.
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
Tips:Input:一个m×n的二维数组,按照顺时针打印
解决办法:每一圈递归一次。
package medium; import java.util.ArrayList;
import java.util.List; public class L54SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
if (matrix == null)
return null;
List<Integer> ans = new ArrayList<>();
int rows = matrix.length;
if (rows==0) return ans;
int cols = matrix[0].length;
int row = 0;
List<Integer> ans1 = spiralOrderCore(matrix, 0, rows - 1, 0, cols - 1, ans);
return ans1;
} private List<Integer> spiralOrderCore(int[][] matrix, int row, int rows, int col, int cols, List<Integer> ans) {
if (row < rows && col < cols) {
for (int c = col; c < cols; c++) {
ans.add(matrix[row][c]);
}
for (int r = row; r < rows; r++) {
ans.add(matrix[r][cols]);
}
for (int i = cols; i > col; i--) {
ans.add(matrix[rows][i]);
}
for (int i = rows; i > row; i--) {
ans.add(matrix[i][col]);
}
spiralOrderCore(matrix, row + 1, rows - 1, col + 1, cols - 1, ans);
} else if (row == rows) {
for (int c = col; c <=cols; c++) {
ans.add(matrix[row][c]);
} } else if (col == cols) {
for (int r = row; r <= rows; r++) {
ans.add(matrix[r][col]);
}
} return ans;
} public static void main(String[] args) {
System.out.println("Main start");
int[][] matrix = { { 1, 2, 3 ,4}, { 5, 6,7, 8 }, { 9,10,11,12 } ,{13,14,15,16}};
int[][] ma={{2,3}};
int[][] matrix1 = { { 1, 2, 3}, {4, 5, 6},{7, 8 , 9}};
L54SpiralMatrix l54 = new L54SpiralMatrix();
List<Integer> ans = l54.spiralOrder(ma);
System.out.println("size:"+ans.size());
for (int i = 0; i <ans.size(); i++) {
System.out.println(ans.get(i));
} }
}
【leetcode】54.Spiral Matrix的更多相关文章
- 【LeetCode】54. Spiral Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
- 【LeetCode】59. Spiral Matrix II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- 【一天一道LeetCode】#54. Spiral Matrix
一天一道LeetCode系列 (一)题目 Given a matrix of m x n elements (m rows, n columns), return all elements of th ...
- 【LeetCode】885. Spiral Matrix III 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【一天一道LeetCode】#59. Spiral Matrix II
一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- [Leetcode][Python]54: Spiral Matrix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...
- LeetCode OJ 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】867 - Transpose Matrix
[题干描述] Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ...
随机推荐
- Leecode刷题之旅-C语言/python-342 4的幂
这里不列举普通的方法了. 发现一个好帖: 学习一下: https://blog.csdn.net/butterfly5211314/article/details/86099993 --------- ...
- hadoop运维笔记
一. 故障处理部分 1.1. spark提交任务报错java.lang.NoClassDefFoundError: com/alibaba/fastjson/JSON 报错原因: 在运行环境没有找 ...
- SSM-CRUD入门项目——新增与校验
新增 分析: 在Index.jsp页面点击新增,弹出对话框(模态框) 数据库查询部门列表显示在模态框中 用户输入数据完成操作 我们先把模态框构建出来,并对新增按钮添加事件,点击按钮弹出模态框: < ...
- PGP的原理与使用
实验名称: PGP的原理与使用 姓名: 刘浩 20155307 任泓霖 20155319 班级: 1553 日期: 2017.10.16 一. 实验环境 Windows虚拟机下使用的是PKZIP2.0 ...
- 2016-2017-2 20155339《 java面向对象程序设计》实验四Android程序设计
2016-2017-2 20155339< java面向对象程序设计>实验四Android程序设计 实验内容 1.Android Stuidio的安装测试: 参考<Java和Andr ...
- 20145209 实验四 《android开发基础》 实验报告
20145209 实验四 <android开发基础> 实验报告 实验内容 Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBI ...
- day3 直方图
1.绘制直方图 # coding=utf-8 import cv2 import numpy as np from matplotlib import pyplot as plt img1 = cv2 ...
- 3110: [Zjoi2013]K大数查询
3110: [Zjoi2013]K大数查询 https://lydsy.com/JudgeOnline/problem.php?id=3110 分析: 整体二分+线段树. 两种操作:区间加入一个数,区 ...
- 源码阅读-GlobalTimer
最近看到一篇文章推了一个开源项目,GlobalTimer.主要是可以用一个定时器来统一管理多个定时任务,可以针对特定任务进行管理. 一.原理 1.一个公共的timer 2.封装任务到自定义个Event ...
- Struts 2(八):文件上传
第一节 基于Struts 2完成文件上传 Struts 2框架中没有提供文件上传,而是通过Common-FileUpload框架或COS框架来实现的,Struts 2在原有上传框架的基础上进行了进一步 ...