054 Spiral Matrix 旋转打印矩阵
给出一个 m x n 的矩阵(m 行, n 列),请按照顺时针螺旋顺序返回元素。
例如,给出以下矩阵:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
应该返回 [1,2,3,6,9,8,7,4,5]。
详见:https://leetcode.com/problems/spiral-matrix/description/
Java实现:
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res=new ArrayList<Integer>();
if(matrix==null||matrix.length==0){
return res;
}
int row=matrix.length;
int col=matrix[0].length;
int top=0;
int bottom=row-1;
int left=0;
int right=col-1;
while(top<=bottom&&left<=right){
for(int j=left;j<=right;++j){
res.add(matrix[top][j]);
}
++top;
for(int i=top;i<=bottom;++i){
res.add(matrix[i][right]);
}
--right;
if(top<=bottom){
for(int j=right;j>=left;--j){
res.add(matrix[bottom][j]);
}
}
--bottom;
if(left<=right){
for(int i=bottom;i>=top;--i){
res.add(matrix[i][left]);
}
}
++left;
}
return res;
}
}
C++实现:
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res;
if (matrix.empty())
return res;
int row = matrix.size();
int col = matrix[0].size();
int top = 0;
int bottom = row - 1;
int left = 0;
int right = col - 1;
//螺旋曲线,运动轨迹总是一致的
while (top <= bottom && left <= right)
{
//向右列递增遍历
for (int j = left; j <= right; j++)
{
res.push_back(matrix[top][j]);
}
top++; //遍历后,去掉此行
//向下行递增遍历
for (int i = top; i <= bottom; i++)
{
res.push_back(matrix[i][right]);
}
right--; //遍历后,去掉此列
if (top <= bottom) //重要判断,防止重复
{
//向左列递减遍历
for (int j = right; j >= left; j--)
{
res.push_back(matrix[bottom][j]);
}
}
bottom--; //遍历后,去掉此行
if (left <= right) //重要判断,防止重复
{
//向上行递减遍历
for (int i = bottom; i >= top; i--)
{
res.push_back(matrix[i][left]);
}
}
left++; //遍历后,去掉此列
}
return res;
}
};
054 Spiral 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] 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 ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- [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 II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 059 Spiral Matrix II 旋转打印矩阵 II
给出正整数 n,生成正方形矩阵,矩阵元素为 1 到 n2 ,元素按顺时针顺序螺旋排列.例如,给定正整数 n = 3,应返回如下矩阵:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6 ...
- 【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(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 054. Spiral Matrix
题目链接:https://leetcode.com/problems/spiral-matrix/description/ Given a matrix of m x n elements (m ro ...
随机推荐
- Nginx HTTP Server相关
一.Nginx安装: 采取手动编译安装 对多种重要的选项进行配置 安装前提:常用工具和库,GCC PCRE(Rewrite模块需要) pcre-devel(源码) zlib zlib-devel(源码 ...
- div靠右浮动案例
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- MySQL上周新增激活用户在上周下单情况_20161107周一
上周新增激活用户在上周下单情况 1.上周激活用户明细 #上周激活用户明细 SELECT a.城市,a.用户ID,a.用户名称,b.用户地址,b.联系电话,a.订单日期,c.年周,c.上周一,a.订单I ...
- [转]从onload和DOMContentLoaded谈起
这篇文章是对这一两年内几篇dom ready文章的汇总(文章的最后会标注参考文章),因为浏览器进化的关系,可能他们现在的行为与本文所谈到的一些行为不相符.我也并没有一一去验证,所以本文仅供参考,在具体 ...
- RT-Thread RTOS
RT-ThreadRTOS是一款来自中国的开源实时操作系统,由RT-Thread工作室的专业开发人员开发.维护. 起初RT-Thread是一个实时的内核(全抢占优先级调度,调度器时间复杂度O(1)), ...
- Ubuntu——跟新flash
使用命令: apt-get install adobe-flashplugin 即可
- JavaScript高级程序设计学习笔记第六章--面向对象程序设计
1.ECMAScript没有类的概念,ECMA-262 把对象定义为:“无序属性的集合,其属性可以包含基本值.对象或者函数.”,有点类似于散列表 2.ECMAScript 中有两种属性:数据属性和访问 ...
- Collection与Map总结
顺序表和链表统称为线性表:顺序表一般表现为数组,如:ArrayList的实现;链表有单链表.双链表.循环链表的区分,如:LinkedArrayList由双链表+哈希表实现
- struts2+jquery+easyui+datagrid+j…
一.概述 struts2提供了针对json的插件支持.常规来讲我们将如何将对象数组转成json对象在客户端直接调用呢?尤其和jquery的easyui插件配合使用,这个可能会有很多的问题需要我们解决. ...
- 百度地图API图标、文本、图例与连线
百度地图开放平台功能强大,使用简单,为地图的自定义提供了非常方便的途径! 本文以绘制一张全国机器辐射图为例记录其基本使用方法,效果如下图: 图中包括了带图标和文本的标注,连线以及图例. 1.关于坐标 ...