【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 ...
随机推荐
- R语言学习笔记(二十四):plyr包的用法
plyr 这个包,提供了一组规范的数据结构转换形式. Input/Output list data frame array list llply() ldply() laply() data fram ...
- 【LG4585】[FJOI2015]火星商店问题
[LG4585][FJOI2015]火星商店问题 题面 bzoj权限题 洛谷 \(Notice:\) 关于题面的几个比较坑的地方: "一天"不是一个操作,而是有0操作就相当于一天开 ...
- python 内置模块(sys)
sys.argv 命令行参数List,第一个元素是程序本身路径sys.exit(n) 退出程序,正常退出时exit(0)sys.version 获取Py ...
- 【Windows定时关机】windows实现定时关机与取消
背景:本人昨晚本来打算将电脑设置为晚上12点 30定时关机,结果写成了:12:30,所以就在刚才,我正玩游戏的时候, 电脑弹出提示:“windows将在一分钟内关闭”,我刚开始一脸懵逼,后来打开昨天敲 ...
- 【转】linux下,如何把整个文件夹上传到服务器(另一台linux)
原文转自:https://zhidao.baidu.com/question/1046040541327493019.html 1.Linux下目录复制:本机->远程服务器 scp -r /h ...
- Python运维三十六式:用Python写一个简单的监控系统
市面上有很多开源的监控系统:Cacti.Nagios.Zabbix.感觉都不符合我的需求,为什么不自己做一个呢? 用Python两个小时徒手撸了一个简易的监控系统,给大家分享一下,希望能对大家有所启发 ...
- xshell连接虚拟机linux系统失败问题
问题:在xshell新建对话弹出的对话框中输入ip地址后,确定并没有弹出输入用户名和密码对话框 直接显示连接失败 Could not connect to ): Connection failed. ...
- 二叉树的宽度<java版>
二叉树的宽度 思路:层序遍历的时候,记录每层的节点数量,最后取记录中的最多的数量. 代码实现: public int solution(TreeNode node){ LinkedList<Tr ...
- python-python爬取妹子图片
# -*- conding=utf-8 -*- import requests from bs4 import BeautifulSoup import io url = "https:// ...
- 实现短信超链接调起APP
因APP推广的需求,需要给APP用户定期发送短信提醒登录使用,为了更好的用户体验在短信内容中嵌入了可以直接打开APP的超链接,下面介绍一下具体的代码实现. 编辑openApp.html文件: < ...