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的更多相关文章

  1. LeetCode 54. 螺旋矩阵(Spiral Matrix) 剑指offer-顺时针打印矩阵

    题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, ...

  2. LeetCode(59):螺旋矩阵 II

    Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 11/8 <matrix> LC 48 54 59

    48. Rotate Image 先按对角线对称图形,再水平对折. class Solution { public void rotate(int[][] matrix) { //1.transpos ...

  7. 【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 ...

  8. 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【刷题-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 ...

随机推荐

  1. Ubuntu环境下手动配置openSSH

    配置openSSH 1.手动下载压缩文件(.tar.gz) zlib-1.2.7.tar.gz openssl-1.0.1j.tar.gz openssh-6.0p1.tar.gz 2.安装zlib ...

  2. 解决ListView 跟ScroolView 共存 listItem.measure(0, 0) 空指针

    在网上找到ListView 和ScroolView 共存的方法无非是给他每个listview 重新增加高度,但是android 的设计者始终认为这并不是一种好的实现方法.但是有的时候有必须要用这种蛋疼 ...

  3. Amzon MWS API开发之订单接口

    Amazon订单接口是Amazon MWS 开发接口中的一大块,我们可以通过接口调用来获得订单数据. 在调用接口之前,首先我们要获得相关店铺商家的店铺密钥等信息.如下: 在此我将所有信息定义在一个类中 ...

  4. web客户端通过ajaxfileupload方式上传文件

    fileUpload.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  5. linux配置防火墙详细步骤(iptables命令使用方法)

    通过本教程操作,请确认您能使用linux本机.如果您使用的是ssh远程,而又不能直接操作本机,那么建议您慎重,慎重,再慎重! 通过iptables我们可以为我们的Linux服务器配置有动态的防火墙,能 ...

  6. editplus的配置文件来支持sql语法高亮【转】

      editplus默认是没有sql语法高亮的,原因是它的内部没有sql.stx的这样一个语法文件 我们自己在 EditPlus 的安装目录下面新建一个文件名为sql.stx,然后打开editplus ...

  7. 强强合体:Docker版Kali Linux发布

    Kali Linux是一款开源的基于Debian的渗透测试专用操作系统,系统中包含一系列用于渗透测试的神器.最近,Kali的开发者们为喜爱Docker的童鞋们发布了新版本. FreeBuf百科:什么是 ...

  8. Upload/download/UrlConnection/URL

    文件上传的核心点 1:用<input type=”file”/> 来声明一个文件域.File:_____ <浏览>. 2:必须要使用post方式的表单. 3:必须设置表单的类型 ...

  9. Android开发之隐式Intent中Intent-filter的三个属性-action,category,data

    使用隐式Intent时,需要使用到意图过滤器Intent-filter.Intent-filter含有三个属性:action,category,data.通过这三个属性的组合,可以启动想要启动的act ...

  10. 函数fsp_alloc_seg_inode_page

    分配一个新的inode page /**********************************************************************//** Allocat ...