LeetCode——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].
原题链接:https://oj.leetcode.com/problems/spiral-matrix/
向右螺旋式遍历。
public class SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
if (matrix.length == 0)
return null;
List<Integer> list = new ArrayList<Integer>();
int l = 0, r = matrix[0].length - 1;
int u = 0, d = matrix.length - 1;
while (l <= r && u <= d) {
for (int i = l; i <= r; i++)
list.add(matrix[u][i]);
u++;
if (u > d)
continue;
for (int i = u; i <= d; i++)
list.add(matrix[i][r]);
r--;
if (l > r)
continue;
for (int i = r; i >= l; i--)
list.add(matrix[d][i]);
d--;
if (u > d)
continue;
for (int i = d; i >= u; i--)
list.add(matrix[i][l]);
l++;
}
return list;
}
}
LeetCode——Spiral Matrix的更多相关文章
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- [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:Spiral Matrix I II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- [LeetCode]Spiral Matrix 54
54.Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the ma ...
- LeetCode: Spiral Matrix 解题报告
Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- [leetcode]Spiral Matrix II @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...
- [leetcode]Spiral Matrix @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix/ 题意: Given a matrix of m x n elements (m rows, n ...
随机推荐
- android:强大的图像下载和缓存库Picasso
1.Picasso一个简短的引论 Picasso它是Square该公司生产的一个强大的图像下载并缓存画廊.官方网站:http://square.github.io/picasso/ 仅仅须要一句代码就 ...
- MSXML2;System.ServiceModel.Configuration;对应dll的添加方法
- Android技术——切换视图(两)随着ViewPage达到Tab幻灯片浏览
Android技术--切换视图(一)~(四)在资源项目:https://github.com/YongYuIT/MeiNv_Liulanqi 一.早期android(android.support.v ...
- 右键菜单中的好友列表Ajax直接跳转请求到登陆页面
今天,我们正在做正确的菜单.当点击重命名Ajax要求,并且不发送数据的背景,但直接跳到主页. 我百思不得其解,后来我发现在头版的一个问题: <li><a href='#' oncli ...
- 策略模式Strategy——坐什么车回家?
1.存在的问题和模型 :2014年6月 学校:廊坊师范 家:石家庄 人物:学生 又快到期末考试了.回家的节奏也奔上日程,无聊之余就想想这次回家的事儿. 对我来说回 ...
- C#基础之二
C#基础之二 1. 静态字段.静态成员:由该类的所有实例共享,其变化,实例是对所有可见. 2. 声明为static有:场.方法.属性.构造函数.运营商.事件. 3. 不同的常量静态量:位置,在编 ...
- 谁占用了我的Buffer Pool
原文:谁占用了我的Buffer Pool 转自:http://blogs.msdn.com/b/apgcdsd/archive/2011/01/11/buffer-pool.aspx 我在做SQL S ...
- 弹出层 div dialog
写你自己的弹出框 风格,如下面 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWluZ2xpYW5sdWFu/font/5a6L5L2T/fontsize ...
- 走向DBA[MSSQL篇] 积跬步行千里
原文:走向DBA[MSSQL篇] 积跬步行千里 不知道大家对SQL系列的感不感兴趣 先在这里探个路 本文针对的读者为SQL菜鸟 欢迎大牛驳论或者补充 既然是探路篇 就先说下数据过滤中的偏门匹配 希望能 ...
- iOS_21团购_发送请求【点评】数据
结果表明,一个简单的请求: 用到的点评封装的类: 使用tableView简单展示: // // DealListController.m // 帅哥_团购 // // Created by beyon ...