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), 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]
.
class Solution {
public:
bool check(vector<vector<int> >& matrix, vector<vector<bool> >& vis, int x, int y) {
if(x< || x>=matrix.size() || y< || y>=matrix[].size() || vis[x][y]) return false;
return true;
}
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res; res.clear();
if(matrix.size() == ) return res; vector<vector<bool> > vis(matrix.size(), vector<bool>(matrix[].size(), false)); int dir[][] = {{,}, {,}, {,-}, {-,}};
int __dir = , sx = , sy = , tot = matrix.size() * matrix[].size(), cnt = ;
while(cnt <= tot) {
res.push_back(matrix[sx][sy]);
vis[sx][sy] = true;
++cnt;
if(!check(matrix, vis, sx+dir[__dir][], sy+dir[__dir][])) __dir = (__dir+)%;
sx = sx + dir[__dir][];
sy = sy + dir[__dir][];
} return res;
}
};
https://leetcode.com/problems/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:
bool check(int n, vector<vector<bool> >& vis, int x, int y) {
if(x< || x>=n || y< || y>=n || vis[x][y]) return false;
return true;
}
vector<vector<int>> generateMatrix(int n) {
vector<vector<int> > res; res.resize();
if(n == ) return res; res.resize(n);
for(int i=;i<res.size();++i) res[i].resize(n);
vector<vector<bool> > vis(n, vector<bool>(n, false)); int dir[][] = {{,}, {,}, {,-}, {-,}};
int sx = , sy = , __dir = , tot = n * n, cnt = ; while(cnt <= tot) {
res[sx][sy] = cnt;
vis[sx][sy] = true;
++cnt;
if(!check(n, vis, sx + dir[__dir][], sy + dir[__dir][])) __dir = (__dir + ) % ;
sx = sx + dir[__dir][];
sy = sy + dir[__dir][];
} return res;
}
};
leetcode@ [54/59] Spiral Matrix & Spiral Matrix II的更多相关文章
- LeetCode 54. 螺旋矩阵(Spiral Matrix) 剑指offer-顺时针打印矩阵
题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, ...
- LeetCode(59):螺旋矩阵 II
Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...
- 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 ...
- 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 ...
- 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 ...
- 11/8 <matrix> LC 48 54 59
48. Rotate Image 先按对角线对称图形,再水平对折. class Solution { public void rotate(int[][] matrix) { //1.transpos ...
- 【LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【刷题-LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
随机推荐
- hdu 1085
额 母函数 #include <cstdio> #include <cstring> int a[3],b[3]= {1,2,5}; int c1[10001],c2[1 ...
- c++内存中字节对齐问题详解
一.什么是字节对齐,为什么要对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址 ...
- POJ3096Surprising Strings(map)
题意:输入很多字符串,以星号结束.判断每个字符串是不是“Surprising Strings”,判断方法是:以“ZGBG”为例,“0-pairs”是ZG,GB,BG,这三个子串不相同,所以是“0-un ...
- Java泛型:类型擦除
类型擦除 代码片段一 Class c1 = new ArrayList<Integer>().getClass(); Class c2 = new ArrayList<String& ...
- HDU1171——Big Event in HDU(母函数)
Big Event in HDU DescriptionNowadays, we all know that Computer College is the biggest department in ...
- SDUT2157——Greatest Number(STL二分查找)
Greatest Number 题目描述Saya likes math, because she think math can make her cleverer.One day, Kudo invi ...
- RabbitMQ安装和配置
RabbitMQ: MQ:message queue.MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来 ...
- Windows使用virtualenv搭建flask开发环境
virtualenv: VirtualEnv用于在一台机器上创建多个独立的Python虚拟运行环境,多个Python环境相互独立,互不影响,它能够: 在没有权限的情况下安装新套件 不同应用可以使用不同 ...
- Target host is not specified错误
对于httpClient4.3访问指定页面,可以从下面的demo抽取方法使用. 注意:对于URL必须使用 http://开始,否则会有如下报错信息: 或者在设置cookie时带上domain: coo ...
- mvc 相关js
http://modernizr.com/ https://github.com/Modernizr/Modernizr/wiki 主要看下Polyfills 用于html5,用于一些老ie,fire ...