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. smarty 时间格式化date_format

    代码如下:$smarty = new Smarty; $smarty->assign('yesterday', strtotime('-1 day')); $smarty->display ...

  2. 【C++基础】 多态 虚函数

    多态:同样的消息被不同类型的对象接收时导致不同的行为.这里“消息”是对类的成员函数的调用,“行为”调用了不同的函数. 分类:①重载多态 ②包含多态……等 实现:编译时的多态  运行时的多态(动态绑定) ...

  3. 【BZOJ 1045】 1045: [HAOI2008] 糖果传递

    1045: [HAOI2008] 糖果传递 Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数n& ...

  4. shorter concat [reverse longer]

    shorter concat [reverse longer] Description: Given 2 strings, a and b, return a string of the form:  ...

  5. wsse:InvalidSecurity Error When Testing FND_PROFILE Web Service in Oracle Applications R 12.1.2 from SOAP UI (Doc ID 1314946.1)

    wsse:InvalidSecurity Error When Testing FND_PROFILE Web Service in Oracle Applications R 12.1.2 from ...

  6. git log

    http://git-scm.com/book/zh/v2 https://backlogtool.com/git-guide/tw/contents/     http://gitbook.liuh ...

  7. awesome-java

    Awesome Java A curated list of awesome Java frameworks, libraries and software. Awesome Java Ancient ...

  8. 理解Java对象序列化(二)

    关于Java序列化的文章早已是汗牛充栋了,本文是对我个人过往学习,理解及应用Java序列化的一个总结.此文内容涉及Java序列化的基本原理,以及多种方法对序列化形式进行定制.在撰写本文时,既参考了Th ...

  9. mysql 读取硬盘数据

    innodb 的最小管理单位是页 innodb的最小申请单位是区,一个区 1M,内含64个页,每个页16K ,即 64*16K=1M, 考虑到硬盘局部性,每次读取4个区,即读4M的数据加载至内存 线性 ...

  10. Unity3D GUI中的图片跟随鼠标旋转脚本

    var Mid : Texture2D; var mouse : Texture2D; //鼠标图片 var mousePs = Vector2.zero; //鼠标的位置 private var a ...