Leetcode: Spiral Matrix. Java
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].
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<Integer>();
int m = matrix.length;
if(m == 0) return res;
int n = matrix[0].length;
//循环次数小于m/2和n/2
for(int i = 0; i < m/2 && i < n/2; i++){
for(int j = 0; j < n - 1 - i * 2; j++)
res.add(matrix[i][i+j]);
for(int j = 0; j < m - 1 - i * 2; j++)
res.add(matrix[i+j][n-i-1]);
for(int j = 0; j < n - 1 - i * 2; j++)
res.add(matrix[m-i-1][n-i-1-j]);
for(int j = 0; j < m - 1 - i * 2; j++)
res.add(matrix[m-i-1-j][i]);
}
//循环结束后假设行数/列数是奇数,则还剩一行/列
if(m % 2 != 0 && m <= n){
for(int j = 0; j < n - (m/2) * 2; j++)
res.add(matrix[m/2][m/2+j]);
}
else if(n % 2 != 0 && m > n){
for(int j = 0; j < m - (n/2) * 2; j++)
res.add(matrix[n/2+j][n/2]);
}
return res;
}
}
Leetcode: Spiral Matrix. Java的更多相关文章
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [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 54
54.Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the ma ...
- LeetCode: Spiral Matrix 解题报告
Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...
- LeetCode:Spiral Matrix I II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- 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 II @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...
随机推荐
- 函数alv下的颜色设置
ABAP中的颜色代码是由4位字都组成的 cxyz c:color的简写,颜色代码均以C开头 x:标准色代码,SAP中一共有7个标准色 y:反转颜色启用/关闭 1/0 z:增强颜色启用/关闭 ...
- 控制台程序的参数解析类库 CommandLine
C#控制台程序的参数解析类库 CommandLine简单使用说明 前言 C#开发的控制台程序,默认接收string[] args参数.如果有多个参数需要输入时,可以按照顺序依次输入:但如果有些参数不是 ...
- http2.0笔记
二进制分帧层 定义了如何封装 HTTP 消息并在客户端与服务器之间传输 http2.0的消息传输特点: 流 已建立的连接上的双向字节流 消息 与逻辑消息对应的完整的一系列数据帧 帧 http2.0通信 ...
- 与众不同 windows phone (17) - Graphic and Animation(画图和动画)
原文:与众不同 windows phone (17) - Graphic and Animation(画图和动画) [索引页][源码下载] 与众不同 windows phone (17) - Grap ...
- eclipse weblogic debug 简易配置版
1. eclipse->help->marketplace->search->weblogic 1.1安装对应eclipse版本的weblogic plugin 查看eclip ...
- java Date 和 javascript Date
近期写一个页面.上面要展示下日期. 在Java中生成了Date.然后将这个Date通过velocity送入vm模板其中 代码例如以下: var dates = new Date("$!{pp ...
- c# listview导出excel文件
首先在工程中需要添加对Microsoft Excel office 11.0 object的引用 生成excel的类代码如下 using System; using System.Collection ...
- birt报表报错, There is no report design object available.org.eclipse.birt.report.exception.ViewerExcepti
报错信息例如以下: - There is no report design object available. org.eclipse.birt.report.exception.ViewerExce ...
- Config File Settings Of EF——实体框架的配置文件设置
我亦MSDN 原文地址 http://msdn.microsoft.com/en-us/data/jj556606 Entity Framework allows a number of settin ...
- [cocos2d-x]屏幕自适应解决的方法
近期在写一个项目,要求pc,ipad,andriod平台上都能够执行,所以选择用cocos2d-x来开发. 我们的资源大小是1024*768的,在pc上和苹果上都是没有问题的,但是到了andriod上 ...