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. mysql 总结二(自定义存储过程)

    mysql执行流程: sql命令--->mysql引擎-----(分析)---->语法正确-----(编译)--->可识别命令----(执行)---->执行结果---(返回)- ...

  2. python 100例 (持续更新)

    1.题目:列表转换为字典. 程序源代码: 1 #!/usr/bin/env python 2 # -*- coding: UTF-8 -*- 3 4 i = ['a', 'b'] 5 l = [1, ...

  3. PHP 文件与文件夹的创建和删除操作

    创建文件夹: mkdir("D:/test");可以创建多级目录,如果存在,则会报错 if(!is_dir($path)) {     if(mkdir($path)){      ...

  4. JQuery-EasyUI DataGrid CRUD

    ASP.NET使用EasyUI-DataGrid + ashx + JQuery Ajax:实现数据的增删查改,查询和分页! 数据表: 学生表:学生编号.姓名.性别.班级编号.年龄 班级表:班级编号. ...

  5. 如何解决Mac里面解压后文件名乱码问题

    如果你把Mac当成你的主要工作机器,而你的同事用的都是Windows,有时候交换文档就是一件很痛苦的事,比如今天要说到的问题:当同事传给你一个zip文件,结果你拿过来解压后发现里面有些文件的文件名如果 ...

  6. Swift2.1 语法指南——嵌套类型

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  7. Xcode如何找到默认的生成路径?

    我最近刚刚入门ObjectiveC,在研习<Objective C程序设计(第6版)>一书. 今天看到有关文件和归档的章节,但是我对XCode的生成文件路径并不了解,然后,在调试代码的时候 ...

  8. objective-c-sort-keys-of-nsdictionary-based-on-dictionary-entries

    NSArray *keys = [someDictionary allKeys]; NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NS ...

  9. java常量池存放在哪里

    运行以下方法: public class Test { public static void main(String[] args) { String str = "abc"; c ...

  10. Call to undefined function mysqli_connect()

    PHP5.0后新支持一个mysqli.dll的扩展功能,能让用户更加简洁的调用mysql数据库. 需要在Php.ini配置中将下面代码前的;去掉.extension=php_mysqli.dll