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

思路:

我是直接套用了Spiral Matrix 的代码

class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int>> ans(n, vector<int>(n,)); int num = ;
for(int i = ; i < (n + ) / ; i++)
{
//行 ----->
for(int j = i; j < n - i; j++)
ans[i][j] = num++;
//列 向下
for(int j = i + ; j < n - i; j++)
ans[j][n - - i] = num++;
//行 <-----------
if(n - i - <= i) //列号 一定要比向左时的列号小 防止重复
break;
for(int j = n - i - ; j >= i; j--)
ans[n - i - ][j] = num++;
//列 向上
if(i >= n - - i) //行号 一定要比向下时的行号大 防止重复
break;
for(int j = n - i - ; j >= i + ; j--)
ans[j][i] = num++;
}
return ans;
}
};

另一种写法的:

class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > ret( n, vector<int>(n) );
int k = , i = ;
while( k <= n * n )
{
int j = i;
// four steps
while( j < n - i ) // 1. horizonal, left to right
ret[i][j++] = k++;
j = i + ;
while( j < n - i ) // 2. vertical, top to bottom
ret[j++][n-i-] = k++;
j = n - i - ;
while( j > i ) // 3. horizonal, right to left
ret[n-i-][j--] = k++;
j = n - i - ;
while( j > i ) // 4. vertical, bottom to top
ret[j--][i] = k++;
i++; // next loop
}
return ret;
}
};

【leetcode】Spiral Matrix II (middle)的更多相关文章

  1. 【leetcode】Set Matrix Zeroes(middle)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...

  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. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

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

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

  5. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  7. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  8. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  9. 【leetcode】 search Insert Position(middle)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

随机推荐

  1. 一道Twitter面试题

    在微博上看到的这个问题,忍住没看答案自己解决了.建议没看过的同学也自己先尝试下. “看下面这个图片” 在这个图片里我们有不同高度的墙.这个图片由一个整数数组所代表,数组中每个数是墙的高度.上边的图可以 ...

  2. css的计数器

    更多关于计数器的问题可以参考:https://developer.mozilla.org/zh-CN/docs/Web/Guide/CSS/Getting_Started/Lists

  3. OC第三节——NSArray和NSMutableArray

    1.OC数组和c数组的区别        C语言的数组:            相同类型变量的有序结合. 类型:可以是简答数据类型.构造数据类型                int     a[10 ...

  4. (:: operator)作用域限定符的几种使用

    一.(:: operator)解决局部变量与全局变量重名问题 ; int main() { ; cout<<var<<endl;//local variable cout< ...

  5. C#获取当前时间与同步时间

     http://blog.163.com/ljq086@126/blog/static/549639712010112921658843/ 我们可以通过使用DataTime这个类来获取当前的时间.通过 ...

  6. PHPStorm Xdebug配置

    下载PHSTORM https://download.jetbrains.com/webide/PhpStorm-2016.1.2.exe http://idea.lanyus.com/查找授权服务器 ...

  7. jbuilder的set!方法重构接口

    https://github.com/rails/jbuilder  的set!方法重构接口, 因为grape没法使用 jBuilder 的缓存,所以直接用 Rails 写 API (1)多个图片 i ...

  8. Opencv角点检测

    #include "stdafx.h" #define max_corners 20 int main() { int cornerNum = max_corners; vecto ...

  9. C++和C中的函数如何相互调用

    今天笔试遇到的一题,当时就写了在函数前声明为C,按C编译. 首先是在C中调用C++函数,包括普通函数,重载函数以及成员函数. 对于普通函数,在C++中声明为extern "C",在 ...

  10. Python面试题库

    1.Python输出中文怎么解决 方法一:用encode和decode如:import os.pathimport xlrd,sys Filename=’/home/tom/Desktop/1234. ...