题目一:

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)的更多相关文章

  1. 算法练习--LeetCode--54. Spiral Matrix 100%

      Spiral MatrixMedium Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  2. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  3. [array] leetcode - 54. Spiral Matrix - Medium

    leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...

  4. LeetCode: Spiral Matrix 解题报告

    Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...

  5. PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]

    1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...

  6. C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...

  7. C#LeetCode刷题之#54-螺旋矩阵(Spiral Matrix)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3672 访问. 给定一个包含 m x n 个元素的矩阵(m 行, ...

  8. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  9. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  10. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

随机推荐

  1. 配置远程maven仓库自己的步骤

    ---恢复内容开始--- 1.首先在远程服务器配置jdk.maven环境,这个可以在网上找 2.Nexus 安装与配置.这个见网上博客(https://blog.csdn.net/lewis_007/ ...

  2. zmqSocket 使用和相关java后台准备

    zmqSocket.as 源码地址: http://zmqsocket-as.googlecode.com/svn/ zmqSocket.js 源码地址: http://zmqsocket-js.go ...

  3. TouchSlide - 大话主席

    http://www.superslide2.com/TouchSlide/downLoad.html 首  页如何使用查看参数案例演示下载页面交流反馈SuperSlide TouchSlide - ...

  4. 使用WebStorm将项目部署到IIS

    在WebStorm中打开项目,通常WS会启动一个虚拟服务器并使用如下地址访问 但这样会有一个问题,在局域网内的其他设备,比如手机和其他电脑是不能访问这个地址的,这样就给开发和调试带来了不便.本人也是惭 ...

  5. 第3章 如何编写函数定义 3.7 if特殊表

    这部分来学习下if特殊表,之前学了defun和let,不好意思,博客中没有写但是鄙人已经看了,哈哈. 什么是if表 if条件特殊表是为了让计算机对条件加以判断,然后选择不同的执行路径的. if特殊表的 ...

  6. Python 内建的filter()函数用于过滤序列。

    例如,在一个list中,删掉偶数,只保留奇数,可以这么写: def is_odd(n): return n % 2 == 1 list(filter(is_odd, [1, 2, 4, 5, 6, 9 ...

  7. web开发之html5---html5 动画特效舞动的雨伞

    http://www.cnblogs.com/stoneniqiu/p/4199294.html

  8. ubuntu 1204 安装mysql

    检測本机是否有安装mysql sudo netstat -tap | grep mysql 运行上面的命令之后.看到 mysq 的socket 处于监听状态,说明有成功安装. 安装mysql sudo ...

  9. 创建有提示的ui组件

    using UnityEditor; using UnityEngine; using System.Collections; using Edelweiss.CloudSystem; namespa ...

  10. hdu 5881 Tea (2016 acm 青岛网络赛)

    原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5881 Tea Time Limit: 3000/1000 MS (Java/Others)    Me ...