LeetCode 53 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) {
ArrayList<Integer> al = new ArrayList<Integer>();
if (matrix.length == 0)
return al;
int x1 = 0;
int y1 = 0;
int x2 = matrix.length - 1;
int y2 = matrix[0].length - 1;
while (x1 <= x2 && y1 <= y2) {
// up row
for (int i = y1; i <= y2; ++i)
al.add(matrix[x1][i]);
// right column
for (int i = x1 + 1; i <= x2; ++i)
al.add(matrix[i][y2]);
// bottom row
for (int i = y2 - 1; x2 != x1&& i >= y1; --i)
al.add(matrix[x2][i]);
// left column
for (int i = x2 - 1; y1 != y2 && i > x1; --i)
al.add(matrix[i][y1]); x1++;
y1++;
x2--;
y2--;
} return al;
}
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
LeetCode 53 Spiral Matrix的更多相关文章
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...
- [LeetCode 题解] Spiral Matrix
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
- [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 ...
- LeetCode 885. Spiral Matrix III
原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
随机推荐
- Qt 向word中插入文字(使用QAxWidget和QAxObject)
pro 文件中要加入 CONFIG += qaxcontainer 2. main.cpp #include <QApplication> #include <QAxWidget&g ...
- NEU 1173: 这是物理学的奇迹!! 分解质数
1173: 这是物理学的奇迹!! 题目描述 goagain在做物理电学实验时需要一个2Ω的电阻,但是他发现他的实验台上只剩下了3Ω,4Ω,5Ω,6Ω的电阻若干,于是goagain把两个4Ω的电阻并联起 ...
- tmpfs(/dev/shm)
tmpfs是一种基于内存的文件系统,它和虚拟磁盘ramdisk比较类似像,但不完全相同,和ramdisk一样,tmpfs可以使用RAM,但它也可以使用swap分区来存储.而且传统的ramdisk是个块 ...
- 无法引入import com.sun.management.OperatingSystemMXBean
现象:在JDK的安装包的jre\lib\rt.jar包里确实有这个类com.sun.management.OperatingSystemMXBean,但是就是不能import com.sun.man ...
- WinMM.dll 函数汇总
#include "MMSystem.h" auxGetDevCaps 查询指定的辅助输出设备以确定其性能 auxGetNumDevs ...
- WebService的相关使用
近期公司项目使用WebService ,这里简单做个总结. 事实上详细使用细节有些情况下须要改,还须要看实际情况,须要与server联调,详细沟通. 比方公司连接,非要把envelope.dotNet ...
- C编译: makefile基础
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在编译一个大型项目的时候,往往有很多目标文件.库文件.头文件以及最终的可执行文件. ...
- FZU1608(线段树)
传送门:Huge Mission 题意:给定区间范围[0,N] (2 <= N <= 50000)和M个区间 (1 <= M <= 500000)和这些区间上的权值,求最终并区 ...
- rm -vf `ls |egrep -v "info_20130826-180233.31764|QueryParser.INFO"`
> rm -vf `ls |egrep -v "info_20130826-180233.31764|QueryParser.INFO"`
- HTML表格标签的使用-<table>
<html> <head> <title> 表格标签 </title> <!-- 标签名:table 定义一个表格 子标签:<caption ...