[算法]旋转矩阵问题(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].
解答:
采用从最外层一层一层向内操作的方法。
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if(matrix==null||matrix.length==0||matrix[0].length==0) return res;
//每一圈左上角数字的坐标为(top,left),右下角的数字的坐标为(bottom,right)
int top = 0, left = 0;
int bottom = matrix.length - 1, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
outEdge(res, matrix, left++, top++, right--, bottom--);
}
return res;
}
private static void outEdge(List<Integer> res, int[][] matrix, int left, int top, int right, int bottom) {
if (top == bottom) {
for (int i = left; i <= right; i++) {
res.add(matrix[top][i]);
}
} else if (left == right) {
for (int i = top; i <= bottom; i++) {
res.add(matrix[i][left]);
}
} else {
int curRow = top;
int curCol = left;
while (curCol < right) {
res.add(matrix[top][curCol++]);
}
while (curRow < bottom) {
res.add(matrix[curRow++][right]);
}
while (curCol > left) {
res.add(matrix[bottom][curCol--]);
}
while (curRow > top) {
res.add(matrix[curRow--][left]);
}
}
}
}
题目二:
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
public static int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
int top = 0, left = 0;
int bottom = n - 1, right = n - 1;
int num = 1;
while (left<=right&&top<=bottom) {
num= inputEdge(num, res, left++, top++, right--, bottom--);
}
return res;
}
private static int inputEdge(int num, int[][] res, int left, int top, int right, int bottom) {
int curRow = top;
int curCol = left;
if (top==bottom&&left==right){
res[top][left]=num;
return num;
}
while (curCol < right) {
res[top][curCol++] = num++;
}
while (curRow < bottom) {
res[curRow++][right] = num++;
}
while (curCol > left) {
res[bottom][curCol--] = num++;
}
while (curRow > top) {
res[curRow--][left] = num++;
}
return num;
}
[算法]旋转矩阵问题(Spiral Matrix)的更多相关文章
- 算法练习--LeetCode--54. Spiral Matrix 100%
Spiral MatrixMedium Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- [array] leetcode - 54. Spiral Matrix - Medium
leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...
- LeetCode: Spiral Matrix 解题报告
Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...
- C#LeetCode刷题之#54-螺旋矩阵(Spiral Matrix)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3672 访问. 给定一个包含 m x n 个元素的矩阵(m 行, ...
- [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 - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
随机推荐
- IBM Security AppScan Glass Box:一种全新的漏洞扫描思想
IBM Security AppScan Glass Box:一种全新的漏洞扫描思想 Glass Box 是 IBM Security AppScan Standard Edition(以下简称 Ap ...
- OrCAD16.6中对比两份DSN文件的方法
OrCAD16.6中对比两份改版前后DSN文件的方法 两种方法: (1)第一种用软件对比netlist (2)用orcad自带的对比功能 一.将两份要对比的原理图都生成orTelesis.dll格式的 ...
- background API
语法: background:bg-color bg-image position/bg-size bg-repeat bg-origin bg-clip bg-attachment initial| ...
- Python基础之初识递归
初识递归 递归的定义: 在一个函数里再调用这个函数本身,这种魔性的使用函数的方式就叫做递归. 递归的最大深度--997 递归函数不受外力的阻止会一直执行下去,python为了杜绝此类现象,强制将递归层 ...
- Httpclient 实现带参文件上传
这里直接贴出的是我封装好的doPostFile方法,httpclient 的版本是3.1. public static String doPostFile(String url, Part[] par ...
- kafka eagle 使用教程
下载 地址:http://download.smartloli.org/ github:https://github.com/smartloli/kafka-eagle 环境 Windows: 安装J ...
- C#实战Microsoft Messaging Queue(MSMQ)消息队列(干货)<转>
前言 在使用MSMQ之前,我们需要自行安装消息队列组件!(具体安装方法大家自己搜一下吧) 采用MSMQ带来的好处是:由于是异步通信,无论是发送方还是接收方都不用等待对方返回成功消息,就可以执行余下的代 ...
- linux 上拷贝文件到windows 上 文件出现锁的文件
要在linux上拷贝出文件到windows上,那么文件必须是777的最高权限. chmod wb_redis -R
- xml schema复杂类型
xml schema复杂类型 对于复杂类型,xs:complexType, xs:sequence子节点必须有. <?xml version="1.0"?> <x ...
- Centos date 设置自定义时间
[1]手动修改 (1)设置日期 # date -s 20190315 (2)设置时间 # date -s 15:23:34 (3)设置日期和时间 # date -s "20190315 15 ...