SpiralMatrix:

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].

算法分析:其实就是两重循环遍历,每次都是遍历一圈,然后遍历里面一圈...,需要记录m,n,每次遍历后,m,n都减去2;还要记录坐标x,y;

特例是n=1;m=1;其实m,n为奇数才有这种特例。

public class SpiralMatrix
{
public List<Integer> spiralOrder(int[][] matrix)
{
List<Integer> res = new ArrayList<>();
if(matrix.length == 0 || matrix == null)
{
return res;
}
int m = matrix.length;
int n = matrix[0].length;//得先判断矩阵是否为空,否则数组下标越界
int x = 0, y = 0;
while(m > 0 && n > 0)
{
if(m == 1)
{
for(int i = 0; i < n; i ++)
{
res.add(matrix[x][y++]);
}
break;//别忘了break
}
else if(n == 1)
{
for(int j = 0; j < m; j ++)
{
res.add(matrix[x++][y]);
}
break;
} for(int i = 0; i < n-1; i ++)
{
res.add(matrix[x][y++]);
}
for(int i = 0; i < m-1; i ++)
{
res.add(matrix[x++][y]);
}
for(int i = 0; i < n-1; i ++)
{
res.add(matrix[x][y--]);
}
for(int i = 0; i < m-1; i ++)
{
res.add(matrix[x--][y]);
}
x++;
y++;
m -= 2;
n -= 2;
}
return res;
}
}

SpiralMatrix2:

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 ]
]

算法分析:和上一算法,类似,只不过这是个n*n矩阵,其实可以延伸到m*n矩阵。n*n使清空更简单,特别是n=1的时候。

public class SpiralMatrix2
{
public int[][] generateMatrix(int n)
{
int[][] res = new int[n][n];
if(n == 0)
{
return res;
}
int x = 0, y = 0;
int val = 1;
while(n > 0)
{
if(n == 1)
{
res[x][y] = val;
break;
}
for(int i = 0; i < n-1; i ++)
{
res[x][y++] = val;
val ++;
}
for(int i = 0; i < n-1; i ++)
{
res[x++][y] = val;
val ++;
}
for(int i = 0; i < n-1; i ++)
{
res[x][y--] = val;
val ++;
}
for(int i = 0; i < n-1; i ++)
{
res[x--][y] = val;
val ++;
}
x ++;
y ++;
n -= 2;
}
return res;
}
}

矩阵螺旋遍历Spiral Matrix,Spiral Matrix2的更多相关文章

  1. 59. Spiral Matrix && Spiral Matrix II

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

  2. leetcode@ [54/59] Spiral Matrix & Spiral Matrix II

    https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n columns), r ...

  3. LeetCode 885. Spiral Matrix III

    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...

  4. [LeetCode] Spiral Matrix 螺旋矩阵

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

  5. LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

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

  6. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  7. [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

    On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...

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

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

  9. [Leetcode] spiral matrix ii 螺旋矩阵

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

随机推荐

  1. [LintCode] 二叉树的前序遍历

    The recursive solution is trivial and I omit it here. Iterative Solution using Stack (O(n) time and ...

  2. git base commond

    打开Git Bash 命令:先写 git status, 它会告诉你怎么做 1. git pull  (把git库中代码拉下来)      2. $ git status (查看状态) 3. $ gi ...

  3. sql---如何把sql查询出来的结果当做另一个sql的条件查询,1、语句2、with as

    '; -- table2 的 name 作为 table1的条件 select * from table1 where name in (select name from table2) --如果有多 ...

  4. [PowerShell]–Checking the version of Office installed

    – Create and initialize the object $objExcel = New-Object -ComObject Excel.Application – Query the v ...

  5. Storm-源码分析- spout (backtype.storm.spout)

    1. ISpout接口 ISpout作为实现spout的核心interface, spout负责feeding message, 并且track这些message. 如果需要Spout track发出 ...

  6. MariaDB日志

    1.查询日志:一般来说不开开启(会产生额外压力,并且不一定有价值),query log 记录查询操作:可以记录到文件(file)中也可记录到表(table)中 general_log=ON|OFF g ...

  7. WireShark告诉你ping百度时都发生了什么

    备注:  测试机器为Mac 重点展示本机发出icmp的过程(dns-->arp-->icmp) 本机默认网关 ->route -n get e -n get default rout ...

  8. 阿里云配置mysql远程连接

    默认是不能用客户端远程连接的,阿里云提供的help.docx里面做了设置说明,mysql密码默认存放在/alidata/account.log 首先登录: mysql -u root -h local ...

  9. ORACLE常用性能监控SQL(二)

    查询Oracle正在执行的sql语句及执行该语句的用户 SELECT b.sid oracleID, b.username 登录Oracle用户名, b.serial#, spid 操作系统ID, p ...

  10. LightOJ - 1336 Sigma Function(约数和+整数拆分)

    题干中给出函数公式: 其中pi为n的每个素因数,ei为其个数.设该函数为F(x),其意义为x的约数之和.问在1-n中有多少x,令F(x)为偶数. 分析:设f(p)为(p^(e+1)-1)/(p-1). ...