[算法]旋转矩阵问题(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 ------------------------------------------------------------------- ...
随机推荐
- URL检测脚本
#!/bin/bash# filename : 8_5_1.sh function usage(){ echo "usage:$0 url" exit 1} function ch ...
- lua学习笔记(四)
表达式 算术操作符 +(加法) -(减法) *(乘法) /(除法) ^(指数) %(取模) -(负号) x%1的结果是x的小数部分,x-x%1是整数部分 关系操作符 ...
- iOS 日志系统 本地日志打包上传到服务器
日志系统主要包含两个部分 1.本地保存 我们知道NSLog打印的日志一般都是直接输出到控制台,开发人员可以在控制台直接看到实时打印的log,既然可以在控制台输出,那么能否将日志输出到其他地方呢,比如说 ...
- CSRF--花式绕过Referer技巧
CSRF遇到Referer绕过的情况,有条件限制,不一定所有的Refere验证就可以绕过 1.Refere为空条件下 解决方案: 利用ftp://,http://,https://,file://,j ...
- LeetCode_Minimum Depth of Binary Tree
一.题目 Minimum Depth of Binary Tree My Submissions Given a binary tree, find its minimum depth. The mi ...
- ORA-24408: could not generate unique server group name
一台新虚拟机,CentOS 6.5系统,用lnmp一键安装包安装好Nginx + PHP环境,再安装Oracle客户端,准备搭建PHP连接Oracle,访问oracle.php,测试连接Oracle的 ...
- saltstack之文件管理
1.managed文件管理 /srv/salt/file/managed.sls /tmp/hyxc: file.managed: - source: - salt://files/hyxc - sa ...
- js自己定义插件-选项卡
该功能比較简单.巩固一下jquery插件写法,注意引入的jquery.js . 自己定义插件路径代码例如以下: 页面: <!doctype html> <html> < ...
- solr查询
1.根据字段查询: http://www.360doc.com/content/14/0306/18/203871_358295621.shtml 2.模糊查询: http://www.tuicool ...
- PHP自动加载功能原理解析
前言 这篇文章是对PHP自动加载功能的一个总结,内容涉及PHP的自动加载功能.PHP的命名空间.PHP的PSR0与PSR4标准等内容. 一.PHP自动加载功能 PHP自动加载功能的由来 在PHP开发过 ...