原题地址:https://leetcode.com/problems/spiral-matrix-ii/

class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
if( == n){
vector<vector<int>> coll;
return coll;
}
vector<vector<int>> coll(n, vector<int>(n, ));
int num = ;
for(int i = ; i < n - ; ++i){
for(int j = i; j < n - i; ++j)
coll[i][j] = num++;
for(int j = i + ; j < n - i; ++j)
coll[j][n - i -] = num++;
for(int j = n - i - ; j >= i; --j)
coll[n - i - ][j] = num++;
for(int j = n - i - ; j >= i + ; --j)
coll[j][i] = num++;
}
return coll;
}
};

LeetCode题目:Spiral Matrix II的更多相关文章

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

  2. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  3. Java for LeetCode 059 Spiral Matrix II

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

  4. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  5. 【leetcode】Spiral Matrix II (middle)

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

  6. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

  7. LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

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

  8. LeetCode 58 Spiral Matrix II

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

  9. [leetcode]59. Spiral Matrix II螺旋遍历矩阵2

    Given a positive integer n, generate a square matrix filled with elements from 1 to n^2 in spiral or ...

随机推荐

  1. xshell连接虚拟机CentOS出现eth0 device not found的解决方法

    昨天用xshell连接虚拟机上的centOS老是连接不上,ifconfig eth0 命令显示 device not found.不知道是什么原因... 折腾了好久 网上是各种搜啊 终于找到解决方法了 ...

  2. (13)python 正则表达式

    匹配单个字符 f. o    f和o之间是任意字符   例如:fbo123 .. 任意两个字符 \.用来匹配. 边界匹配 the     表示包含the的任何字符串 ^from 表示以from开头的所 ...

  3. codeforces #441 B Divisiblity of Differences【数学/hash】

    B. Divisiblity of Differences time limit per test 1 second memory limit per test 512 megabytes input ...

  4. 洛谷 P1012 拼数 [字符串]

    题目描述 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3时,3个整数13,312,343联接成的最大整数为:34331213 又如:n=4时,4个整数7,13,4 ...

  5. 洛谷P3929 SAC E#1 - 一道神题 Sequence1【枚举】

    题目描述 小强很喜欢数列.有一天,他心血来潮,写下了一个数列. 阿米巴也很喜欢数列.但是他只喜欢其中一种:波动数列. 一个长度为n的波动数列满足对于任何i(1 <= i < n),均有: ...

  6. 【我要学python】愣头青之初安装就打了一记耳光

    pycharm安装好后创建项目出现interpreter field is empty,导致pycharm无法使用. 这是因为python没有安装好,重新自定义安装一次即可 下载地址:https:// ...

  7. 树链剖分【p2568】[SDOI2011]染色

    Description 给定一颗有\(n\)个节点的无根树和\(m\)个操作,操作有\(2\)类: 1.将节点\(a\)到节点\(b\)路径上所有点染成颜色\(c\) 2.询问节点\(a\)到节点\( ...

  8. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals )D. Innokenty and a Football League(2-sat)

    D. Innokenty and a Football League time limit per test 2 seconds memory limit per test 256 megabytes ...

  9. Placement new的用法及用途【转】

    什么是placement new? 所谓placement new就是在用户指定的内存位置上构建新的对象,这个构建过程不需要额外分配内存,只需要调用对象的构造函数即可.举例来说: class foo{ ...

  10. 8、Django实战第8天:session和cookie自动登录机制

    因为http是无状态协议,因此,并不会记录用户的登录状态.在早期,是直接把用户名和密码等信息存储在浏览器的cookie来实现记录用户密码登录. 但是这样存在安全隐患,只要别人登录你的电脑cookie信 ...