【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 ...
随机推荐
- C语言/C++编程学习:栈的代码实现之数组方案
C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...
- HTTP1.0,HTTP1.1,HTTP2.0的主要特征对比
HTTP1.0 是一种无状态.无连接的应用层协议. HTTP1.0规定浏览器和服务器保持短暂的连接,浏览器的每次请求都需要与服务器建立一个TCP连接,服务器处理完成后立即断开TCP连接(无连接),服务 ...
- sed: unix与doc换行的转换
在Linux (Unix)平台下回车换行以\n表示 在Window平台下回车换行以\r\n表示 两者的差异导致了: 在window下看Linux的文本排版全乱 在Linux在看Window的文本则是存 ...
- Linux 下 终端 相关的命令
1. 概述 Linux 服务器, 通常可以由多个终端连接 简单介绍一些 终端 相关的操作 最终的目的, 是定位到某个终端, 然后把它 踢下来, 甚至可以不让他再次连接 2. 环境 操作系统 CentO ...
- 20155206 2016-2017-2《Java程序设计》课程总结
20155234 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:师生关系 预备作业2:优秀技能经验 预备作业3:虚拟机linux初接触 第一周作业:认识 ...
- mycp 补交作业
老师好:我昨天做完时已经是11点多了,错过了提交时间,希望用此篇博客弥补一下我的过失. import java.io.; import java.lang.; import java.util.Sca ...
- Mysql优化分页
背景: 库里面有张表,日增数据量百万条: 之前查询: SELECT * FROM `res_battery_data_history` LIMIT 1797000,10;
- LUA中点号和冒号的区别
Student = {}; Student.__index = Student; function Student:new(name, age) local temp = {}; setmetatab ...
- HTML中CSS入门基础
HTML.CSS 实用css有三种格式:内嵌:内联:外部: 分类:内联:写在标记的属性位置,优先级最高,重用性最差内嵌:写在页面的head中,优先级第二,重用性一般外部:写在一个以css结尾的文件中, ...
- Python中abs()和math.fabs()区别
描述:Python中fabs(x)方法返回x的绝对值.虽然类似于abs()函数,但是两个函数之间存在以下差异: abs()是一个内置函数,而fabs()在math模块中定义的. fabs()函数只适用 ...