https://oj.leetcode.com/problems/spiral-matrix-ii/

螺旋矩阵,和题目一一样的思路,这个是产生n*n 矩阵。

#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > ans;
if(n == )
return ans;
if(n ==)
{
vector<int> ansPiece;
ansPiece.push_back();
ans.push_back(ansPiece);
return ans;
}
ans.resize(n);
for(int i = ;i<n;i++)
ans[i].resize(n); int l1,l2,r2,p1;
l1 = ;
l2 = ;
r2 = n - ;
p1 = n -; int num = ;
while()
{
int i;
if(num>n*n)
break; for(i = l2; i <= r2; i++)
{
ans[l1][i] = num;
num++;
} if(l1+>p1)
break;
for(i = l1+;i<= p1;i++)
{
ans[i][r2] = num;
num++;
} if(r2-<l2)
break;
for(i = r2-;i>=l2;i--)
{
ans[p1][i] = num;
num++;
} if(p1-<l1+)
break;
for(i = p1-;i>=l1+;i--)
{
ans[i][l2] = num;
num++;
}
l1++;
l2++;
r2--;
p1--;
}
return ans;
}
};

LeetCode OJ-- Spiral Matrix II的更多相关文章

  1. 【leetcode】Spiral Matrix II

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

  2. 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 ...

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

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

  10. LeetCode题目:Spiral Matrix II

    原题地址:https://leetcode.com/problems/spiral-matrix-ii/ class Solution { public: vector<vector<in ...

随机推荐

  1. Spring Security 与 OAuth2(介绍)

    https://www.jianshu.com/p/68f22f9a00ee Spring Security 与 OAuth2(介绍) 林塬 2018.01.23 11:14* 字数 3097 阅读 ...

  2. 常用排序算法的总结以及编码(Java实现)

    常用排序算法的总结以及编码(Java实现) 本篇主要是总结了常用算法的思路以及相应的编码实现,供复习的时候使用.如果需要深入进行学习,可以使用以下两个网站: GeeksForGeeks网站用于学习相应 ...

  3. Python基础——模块与包

    在Python中,可以用import导入需要的模块.包.库.文件等. 把工作路径导入系统路径 import os#os是工作台 import sys#sys是系统 sys.path.append(os ...

  4. Lecture 3

    surface models 1. The two main methods of creating surface models are interpolation and triangulatio ...

  5. Python 简单购物程序

    # Author:Eric Zhao# -*- coding:utf-8 -*-'''需求:启动程序后,让用户输入工资,然后打印商品列表允许用户根据商品编号购买商品用户选择商品后,检测余额是否够,够就 ...

  6. poj 1328 安雷达问题 贪心算法

    题意:雷达如何放置?在xoy二维平面坐标系里面,x轴上方的为岛屿,x轴下方的是雷达要放到位置,如何放使得雷达放的最少? 思路 肯定放在x轴上减少浪费是最好的选择 什么情况下,雷达无法到达呢?--以这个 ...

  7. matlab 画图进阶

    matlab 画图进阶 applications of matlab in engineering 图表类型的选择 first:advanced 2d plots special plots logl ...

  8. java EE技术体系——CLF平台API开发注意事项(2)——后端测试

    前言:上篇博客说到了关于开发中的一些情况,这篇博客主要说明一些关于测试的内容. 一.宏观说明 要求:每一个API都必须经过测试.   备注:如果涉及到服务间调用(如权限和基础数据),而对方服务不可用时 ...

  9. [转]Visual Studio调试之符号文件

    http://www.cnblogs.com/killmyday/archive/2009/10/14/1582882.html 前面在不能设置断点的检查步骤和Visual Studio调试之断点进阶 ...

  10. 一句话学Java——Java重载和重写

    概念:重载是指两个不同的函数有相同的名称,可以是在本类之中的函数之间的重载,也可以是子类和父类的函数之间的函数重载. 重写:只能是子类重写父类的函数.这是多态的基础. 重写的规则:     参数:重写 ...