[算法]旋转矩阵问题(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 ------------------------------------------------------------------- ...
随机推荐
- sql CHARINDEX() 与 PATINDEX() LEN() substring() COLLATE RAISERROR
CHARINDEX() 在一个表达式中搜索另一个表达式并返回其起始位置(如果找到). CHARINDEX ( expressionToFind , expressionToSearch [ , st ...
- web网页按钮如何制作
1:用矩形形状工具画一个矩形 2 : 加描边 3:三键+N新建图层 前景色变成白色,白色到透明的渐变,选择径向渐变. 4:为了使自然,按住ALT键,使渐变扩大,和矩形保持一直,之后向上调整一些. 5: ...
- Squid 启动/停止/重载配置文件 命令
当你的 squid.conf 配置文档按照你的想法修改完以后,启动 squid 之旅就开始了. Squid安装设试命令: 1,初始化你在 squid.conf 里配置的 cache 目录 #/usr/ ...
- SpringCloud系列五:为Eureka Server添加用户认证及元数据
1. 回顾 上一篇博客讲解了Eureka集群及将微服务注册到集群上.在前面的讲解中,Eureka Server都是允许匿名访问的,本次将讲解如何构建一个需要登录才能访问的Eureka Server. ...
- SpringSecurity学习二----------实现自定义登录界面
© 版权声明:本文为博主原创文章,转载请注明出处 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0& ...
- hive中遇到的问题
) from t_1 where country ='China' group by (name = 'qq'); 结果图 select * from t_sz_part; 按照理解来说,应该只有一个 ...
- web开发之html5---html5 动画特效舞动的雨伞
http://www.cnblogs.com/stoneniqiu/p/4199294.html
- git入门五(分支合并冲突和衍合)
分支合并冲突的处理 合并分支的冲突时在不同的分支中修改了同一个文件的同一部分,程序无法把两份有差异的文件合并,这时候需要人为的干预解决冲突.当前处于master 分支,当dev 分支和master ...
- python带setup.py的包的安装
如psutil-4.2.0.tar.gz tar -zxvf psutil-4.2.0.tar.gz psutil-4.2.0 cd psutil-4.2.0 sudo python setup.py ...
- django模板加载静态资源
1. 目录结构 /mysite/setting.py部分配置: # Django settings for mysite project. import os.path TEMPLATE_DIRS = ...