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 ]
]

依旧是dfs问题,代码如下所示:

 class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>>ret(n, vector<int>(n, ));
vector<vector<bool>>mark(n, vector<bool>(n,false));
if(!n) return ret;
dfs(ret, mark, , -, n, , );
return ret;
} void dfs(vector<vector<int>> & ret, vector<vector<bool>> & mark, int i, int j, int n, int curr, int currVal)
{
for(int k = ; k < ; ++k){
int dirct = (curr + k) % ;
int ii = i + drct[dirct][];
int jj = j + drct[dirct][];
if(ii >= && ii < n &&
jj >= && jj < n &&
mark[ii][jj] == false){
mark[ii][jj] = true;
ret[ii][jj] = currVal;
dfs(ret, mark, ii, jj, n, dirct, currVal + );
} }
} private:
vector<vector<int>>drct = {{,},{,},{,-},{-,}};
};

代码写的比较乱,凑合着看哈哈

LeetCode OJ:Spiral MatrixII(螺旋矩阵II)的更多相关文章

  1. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  2. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

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

  4. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  5. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

    Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...

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

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

  7. Java实现 LeetCode 59 螺旋矩阵 II

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

  8. 【LeetCode】59.螺旋矩阵II

    59.螺旋矩阵II 知识点:数组: 题目描述 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . 示例 输入:n = 3 ...

  9. leetcode 54. 螺旋矩阵 及 59. 螺旋矩阵 II

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

  10. 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

    [059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...

随机推荐

  1. JSONObject和JSONArray 以及Mybatis传入Map类型参数

    import org.json.JSONArray;import org.json.JSONObject; 将字符串转化为JSONArray   JSONArray  jsonArray = new ...

  2. JS的深拷贝

    var obj = { name: "wuyongyu", age: 18 } 第一种方式: function deepClone(obj){ // 判断传入的数据类型 - 数组或 ...

  3. JAVA 读取txt文件内容

    原文地址https://www.cnblogs.com/xing901022/p/3933417.html 通常,我们可以直接通过文件流来读取txt文件的内容,但有时可能会出现乱码!此时只要设置一下文 ...

  4. go——工程结构

    Go是一门推崇软件工程理念的编程语言,它为开发周期的每个环节都提供了完备的工具和支持. Go语言高度强调代码和项目的规范和统一,这几种体现在工程结构或者说代码体制的细节之处. 1.工作区 一般情况下, ...

  5. image_Magic图片处理功能

    :] 来自为知笔记(Wiz)

  6. pt-osc原理

    pt-osc原理 1.检查设置环境 测试db是否可连通,并且验证database是否存在 SET SESSION innodb_lock_wait_timeout=1 //InnoDB事务等待行锁的超 ...

  7. Python面试题之集合推导式、字典推导式

    集合推导式 集合推导式(set comprehensions)跟列表推导式也是类似的, 唯一的区别在于它们使用大括号{}表示. Code: sets = {x for x in range(10)} ...

  8. clipbrd剪切板查看器

    本文,我们来学习一下简单的概念,即,如何查看系统剪贴版里面有什么内容?   如果要想看.或者验证系统剪贴版里面都有什么内容,最为简单的方法就是通过"粘贴"的操作来验证!   但是, ...

  9. winform webbrowser禁用网页target=blank

    /// <summary> /// 屏蔽target=_blank 的弹出窗口 /// </summary> /// <param name="sender&q ...

  10. 黑苹果Yosemite 10.10.1懒人版完美安装及简单驱动设置

    1.硬件概要 CPU: 英特尔 Xeon E3-1230 V2 (四核)主板: 技嘉 H77-DS3H (Intel H77 (Panther Point Base))内存: 8 GBytes显卡: ...