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 ]
]
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > result(n, vector<int>(n,));
leftPos = ;
rightPos = n-;
topPos = ;
bottomPos = n-;
currentNum = ;
goWider(result,true);
return result;
}
void goWider(vector<vector<int> > &matrix, bool direct)
{
if(direct)
{
for(int i = leftPos; i<= rightPos; i++)
{
matrix[topPos][i] = currentNum++;
}
topPos++;
if(topPos > bottomPos) return;
goDeeper(matrix, true);
}
else
{
for(int i = rightPos; i>= leftPos; i--)
{
matrix[bottomPos][i] = currentNum++;
}
bottomPos--;
if(topPos > bottomPos) return;
goDeeper(matrix, false);
}
}
void goDeeper(vector<vector<int> > &matrix, bool direct)
{
if(direct)
{
for(int i = topPos; i<= bottomPos; i++)
{
matrix[i][rightPos]=currentNum++;
}
rightPos--;
if(leftPos > rightPos) return;
goWider(matrix, false);
}
else
{
for(int i = bottomPos; i>= topPos; i--)
{
matrix[i][leftPos] = currentNum++;
}
leftPos++;
if(leftPos > rightPos) return;
goWider(matrix, true);
}
}
private:
int currentNum;
int leftPos;
int rightPos;
int topPos;
int bottomPos;
};

思路II:向右向下向左向上,4个for循环。外套一个while,while的结束条件是4个for循环都不满足循环条件了。

class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
int len = n;
int num = ;
vector<vector<int>> ret(n, vector<int>(n,));
while(len>n-len){
for(int i = n-len; i < len; i++){
ret[n-len][i] = num++;
}
for(int j = n-len+; j < len; j++){
ret[j][len-] = num++;
}
for(int k = len-; k >= n-len; k--){
ret[len-][k] = num++;
}
for(int l = len-; l > n-len; l--){
ret[l][n-len] = num++;
}
len--;
}
return ret;
}
};

59. Spiral Matrix II (Array)的更多相关文章

  1. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  2. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  3. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  4. Leetcode#59 Spiral Matrix II

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

  5. 59. Spiral Matrix II

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

  6. LeetCode OJ 59. Spiral Matrix II

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

  7. 59. Spiral Matrix II(中等,同54题)

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

  8. 【一天一道LeetCode】#59. Spiral Matrix II

    一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

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

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

随机推荐

  1. 将int转int数组并将int数组元素处理后转int,实现加密

    package faceobject; import java.util.Arrays; public class Test { /** 加密问题 数据是小于8位的整数 先将数据倒序,然后将每位数字都 ...

  2. Beta阶段第2周/共2周 Scrum立会报告+燃尽图 14

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2411] 版本控制:https://git.coding.net/liuyy08 ...

  3. java并发编程之volatile

    Java语言规范第三版中对volatile的定义如下:Java编程语言允许线程访问共享变量,为了确保共享变量能被准确和一致地更新,线程应该确保通过排他锁单独获得这个变量. 了解volatile关键字之 ...

  4. SqlServer一些常用函数(持续更新。。。)

    1. 字符串拼接: + 拼接 SELECT 'AA' + 'BB' A //AABB在2012版本sqlserver之后,可以使用cancat进行字符串拼接了. 2. 判断是否为空,并取另外的值 :I ...

  5. HDU5296 Annoying problem(LCA)

    //#pragma comment(linker, "/STACK:1677721600") #include <map> #include <set> # ...

  6. O​r​a​c​l​e​ ​D​a​t​a​b​a​s​e​ ​e​x​p​r​e​s​s​ ​1​1​g​ ​第​ ​2​ ​版​安​装和配置

    官方Oracle Database 快捷版 11g 第 2 版的下载地址: http://www.oracle.com/technetwork/cn/products/express-edition/ ...

  7. 【转】详解硬盘MBR

    原文网址:http://hi.baidu.com/waybq/item/a4490f026f9859d21ef046a4 硬盘是现在计算机上最常用的存储器之一.我们都知道,计算机之所以神奇,是因为它具 ...

  8. java内存上堆和栈的一些理解

    多线程上的基本类型:https://blog.csdn.net/championhengyi/article/details/76857401

  9. pfsense openvpn上网终于通了

    先看配置,等会在说过程中遇到的问题: 1.openvpn配置: /var/etc/openvpn/server2.conf下: port 1195 proto tcp dev tap writepid ...

  10. WinCE全屏手写输入法

    来源地址:http://www.dwhand.com/?page_id=570 WinCE全屏手写输入法 功能特点: 1.支持手写识别,智能拼音.仓颉.注音.笔画.五笔.ABC等输入模式. 2.支持各 ...