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 ...
 
随机推荐
- 用css制作三角形
			
用css制作三角形,主要是利用css元素给“盒模型”设置边框得到的. 上图,上边框和做边框,以及上边框和右边框的交合处,浏览器会按照直角的二分之一处绘制交合线.这是“盒模型”有宽和高时候的效果.我们假 ...
 - dll的概念 dll导出变量 函数 类
			
1. DLL的概念 DLL(Dynamic Linkable Library),动态链接库,可以向程序提供一些函数.变量或类.这些可以直接拿来使用. 静态链接库与动态链接库的区别: (1)静态链接 ...
 - 如何在eclipse里使用git
			
新版都自带git插件了.在项目上右键,选team,选share project,再选择git就可以了. 如果在本地使用git比较简单.如果要多人共享的使用git,那么需要专门的服务器,并提供ssh,这 ...
 - [itint5]二叉树转换线索二叉树
			
http://www.itint5.com/oj/#27 用了基于stack的中序遍历,记录一下last,就很简单了. #include <stack> /*树结点的定义(请不要在代码中定 ...
 - eCos中的线程与同步
			
http://blog.csdn.net/ooaven/article/details/6280018 先看一下eCos线程的创建.控制以及优先级的操作这三个方面的知识,主要是对它的实现方式及API做 ...
 - DVB系统中PCR的生成和PCR校正
			
http://blog.csdn.net/chenliangming/article/details/3616720 引自<广播电视信息>2008年1月 从数字电视前端系统功能上来讲,传统 ...
 - c++编写webui内核 .
			
http://blog.csdn.net/sx1989827/article/details/8068779 #pragma once #include <mshtmhst.h> #inc ...
 - Hive QL 介绍
			
小结 本次课程学习了 Hive QL 基本语法和操作. 一.实验环境说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou,密码shiyanlou 2. 环境介绍 本实验环境采用带桌面的 ...
 - poj 1459 Power Network(增广路)
			
题目:http://poj.org/problem?id=1459 题意:有一些发电站,消耗用户和中间线路,求最大流.. 加一个源点,再加一个汇点.. 其实,过程还是不大理解.. #include & ...
 - Linux SocketCan client server demo hacking
			
/*********************************************************************** * Linux SocketCan client se ...