LeetCode OJ:Spiral MatrixII(螺旋矩阵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 ]
]
依旧是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)的更多相关文章
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 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 ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- [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 ...
- LeetCode(59):螺旋矩阵 II
Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...
- Java实现 LeetCode 59 螺旋矩阵 II
59. 螺旋矩阵 II 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ...
- 【LeetCode】59.螺旋矩阵II
59.螺旋矩阵II 知识点:数组: 题目描述 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix . 示例 输入:n = 3 ...
- leetcode 54. 螺旋矩阵 及 59. 螺旋矩阵 II
54. 螺旋矩阵 问题描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, ...
- 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】
[059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...
随机推荐
- 正则表达式 - JavaScript描述
正则表达式 - JavaScript描述 概述 正则表达式是被用来匹配字符串中的字符组合的模式.在JavaScript中,正则表达式也是对象. 创建正则表达式 var re = /abc/; // 使 ...
- Python HTML Resolution Demo - SGMLParser & PyQuery
1. SGMLParser: 这里定义了一个Parse类,继承SGMLParser里面的方法.使用一个变量is_h4做标记判定html文件中的h4标签,如果遇到h4标签,则将标签内的内容加入到Pars ...
- mongo常用查询
复杂查询: and: or: lte,gte,=: and+lt: , 逗号表示and, $lt小于写在值当中 查询实例: 找到含有指定数据文档 查找条件spcode有1个字符长度的文档 db.sp ...
- PAT 甲级真题
1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...
- 【转】Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- C#转义字符(好记性不如烂笔头)
C#转义字符: ·一种特殊的字符常量:·以反斜线"\"开头,后跟一个或几个字符.·具有特定的含义,不同于字符原有的意义,故称“转义”字符.·主要用来表示那些用一般字符不便于表示的控 ...
- cdoj1325卿学姐与基本法
地址:http://acm.uestc.edu.cn/#/problem/show/1325 题目: 卿学姐与基本法 Time Limit: 2000/1000MS (Java/Others) ...
- linux例行性工作调度学习(一)
Linux系统中有一种例行性工作(crontab)可以调度,是通过crontab和at来实现的. 这两种工作调度: 一种是例行性的,就是每隔一定的周期要来办的事项. 一种是突发性的,就是这次做完以后就 ...
- iOS 多线程安全 与可变数组
完全来自于iOS 多线程安全与可变字典 的学习 基本相同,举一反三 直接上样例代码 是我参照网上,根据当前业务需求改的. 其实好多人在这里喜欢用类别处理.我个人觉得用类别 极其容易和普通方法混淆,所以 ...
- rabbitmq的发布确认和事务
摘要: 介绍confirm的工作机制.使用spring-amqp介绍事务以及发布确认的使用方式.因为事务以及发布确认是针对channel来讲,所以在一个连接中两个channel,一个channel可以 ...