LeetCode:Spiral Matrix I II
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]
.
打印螺旋矩阵
逐个环的打印, 对于m *n的矩阵,环的个数是 (min(n,m)+1) / 2。对于每个环顺时针打印四条边。
注意的是:最后一个环可能只包含一行或者一列数据
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
int m = matrix.size(), n;
if(m != 0)n = matrix[0].size();
int cycle = m > n ? (n+1)/2 : (m+1)/2;//环的数目
vector<int>res; int a = n, b = m;//a,b分别为当前环的宽度、高度
for(int i = 0; i < cycle; i++, a -= 2, b -= 2)
{
//每个环的左上角起点是matrix[i][i],下面顺时针依次打印环的四条边
for(int column = i; column < i+a; column++)
res.push_back(matrix[i][column]);
for(int row = i+1; row < i+b; row++)
res.push_back(matrix[row][i+a-1]);
if(a == 1 || b == 1)break; //最后一个环只有一行或者一列
for(int column = i+a-2; column >= i; column--)
res.push_back(matrix[i+b-1][column]);
for(int row = i+b-2; row > i; row--)
res.push_back(matrix[row][i]);
}
return res;
}
};
Spiral Matrix II
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 ]
]
本质上和上一题是一样的,这里我们要用数字螺旋的去填充矩阵。同理,我们也是逐个环的填充,每个环顺时针逐条边填充 本文地址
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > matrix(n, vector<int>(n));
int a = n;//a为当前环的边长
int val = 1;
for(int i = 0; i < n/2; i++, a -= 2)
{
//每个环的左上角起点是matrix[i][i],下面顺时针依次填充环的四条边
for(int column = i; column < i+a; column++)
matrix[i][column] = val++;
for(int row = i+1; row < i+a; row++)
matrix[row][i+a-1] = val++;
for(int column = i+a-2; column >= i; column--)
matrix[i+a-1][column] = val++;
for(int row = i+a-2; row > i; row--)
matrix[row][i] = val++;
}
if(n % 2)matrix[n/2][n/2] = val;//n是奇数时,最后一个环只有一个数字
return matrix;
}
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3774747.html
LeetCode:Spiral Matrix I II的更多相关文章
- 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 ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 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 ...
- Spiral Matrix I & II
Spiral Matrix I Given an integer n, generate a square matrix filled with elements from 1 to n^2 in s ...
- [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 @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...
- Leetcode Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
- 1034. Head of a Gang (30)
分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...
- 反射生成SQL语句
public static int Reg(Model ml) { bool b = true; Visit vt = new Visit(); StringBuilder builder = new ...
- sqlplus启动后的环境SQLPATH的设置
sqlplus启动时会查找和加载的两个文件login.sql和glogin.sql.其中glogin.sql文件默认存放在$ORACLE_HOME/sqlplus/admin目录下,login.sql ...
- oracle 查看运行中sql
sys用户登录 SELECT b.sid oracleID, b.username 登录Oracle用户名, b.serial#, spid 操作系统ID, paddr, sql_text 正在执行的 ...
- 不制作证书是否能加密SQLSERVER与客户端之间传输的数据?
不制作证书是否能加密SQLSERVER与客户端之间传输的数据? 在做实验之前请先下载network monitor抓包工具 微软官网下载:http://www.microsoft.com/en-us/ ...
- 用c#开发微信 (17) 微活动 3 投票活动 (文本投票)
前面介绍了微活动<大转盘> 和 <刮刮卡>,这次介绍下微投票,微投票分二种,一种是文本投票, 一种是图片投票. 下面介绍文本投票的详细步骤: 1. 新建文本投票活动 ...
- ASP.NET Core 源码阅读笔记(3) ---Microsoft.AspNetCore.Hosting
有关Hosting的基础知识 Hosting是一个非常重要,但又很难翻译成中文的概念.翻译成:寄宿,大概能勉强地传达它的意思.我们知道,有一些病毒离开了活体之后就会死亡,我们把那些活体称为病毒的宿主. ...
- 自己动手写客户端UI库——创建第一个控件
在上一篇文章中我们主要讲了C#如何和JS通信, 这一篇文章中,我们将创建一个最基础的Button控件 WUI库中控件的继承机制 我们先解释最简单的继承机制,以后WUI库的继承机制会比这个复杂的多 ...
- jquery 常用插件
50款最有用的 jQuery 插件集锦<表单篇> 50款最有用的 jQuery 插件集锦<网页布局篇> 50款最有用的 jQuery 插件集锦<内容滑块篇> 50款 ...
- [安卓] 9、线程、VIEW、消息实现从TCP服务器获取数据动态加载显示
一.前言: 一般情况下从TCP服务器读取数据是放在一个线程里读的,但是刷新界面又不得不放在线程外面,所以需要用消息传递把线程里从TCP里获得的数据传送出来,然后根据数据对页面进行相应的刷新. 二.业务 ...