LeetCode Spiral Matrix II (技巧)
题意:
从1开始产生连续的n2个数字,以螺旋的方式填满一个n*n的数组。
思路:
由于是填满一个矩阵,那么只需要每次都填一圈即可。应该注意特殊情况。
迭代:
class Solution {
public:
vector<vector<int> > generateMatrix(int n)
{
vector<vector<int> > ans(n,vector<int>(n));
int cnt=, i=;
while()
{
if(cnt==n*n) break;
for(int j=i; j<n-i; j++) ans[i][j]=++cnt;
for(int j=i+; j<n-i; j++) ans[j][n-i-]=++cnt;
for(int j=n-i-; j>=i; j--)ans[n-i-][j]=++cnt;
for(int j=n-i-; j>i; j--) ans[j][i]=++cnt;
i++;
}
return ans;
}
};
AC代码
递归:
class Solution {
public:
vector<vector<int> > ans;
int n;
void generate(int i,int cnt)
{
if(cnt==n*n) return ;
for(int j=i; j<n-i; j++) ans[i][j]=++cnt;
for(int j=i+; j<n-i; j++) ans[j][n-i-]=++cnt;
for(int j=n-i-; j>=i; j--)ans[n-i-][j]=++cnt;
for(int j=n-i-; j>i; j--) ans[j][i]=++cnt;
generate(i+,cnt);
}
vector<vector<int> > generateMatrix(int n)
{
ans=vector<vector<int> >(n,vector<int>(n));
this->n=n;
generate(, );
return ans;
}
};
AC代码
LeetCode Spiral Matrix II (技巧)的更多相关文章
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- [leetcode]Spiral Matrix II @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...
- Leetcode Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 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 ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- LeetCode:Spiral Matrix I II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
随机推荐
- Google Chrome 源码下载地址 (Google Chrome Source Code Download)
1. Google Chrome 源码 SVN 地址:http://src.chromium.org/svn.包含有 Chrome.Gears.Webkit.GCC 等源码以及编译依赖工具.Chrom ...
- JBOSS常用端口说明
1.jboss 的端口修改位置总结 Jboss通常占用的端口是1098,1099,4444,4445,8080,8009,8083,8093这几个, 默认端口是8080 在windows系统中: 10 ...
- 一个app中保持程序全屏的方法。
public void toggleFullscreen(boolean fullScreen) { //fullScreen为true时全屏 WindowManager.LayoutParams a ...
- 线程系列3---ThreadLocal类研究
2013-12-23 17:44:44 Java为线程安全提供了一些工具类,如ThreadLocal类,它代表一个线程局部变量,通过把数据放在ThreadLocal中就可以让每个线程创建一个该变量的副 ...
- C-指针和数组的区别
指针的操作: 允许:1)同类型指针的赋值 2)与整形的加减运算 3)指向同一数组内指针的减运算和比较 4)赋 ‘0’ 或与 ‘0’ 比较 不允许:1)两指针的相加,相乘除,位移或mask 2)与flo ...
- tomcat 详解
首先搞清楚几个概念:Servlet容器与web容器.Servlet容器的主要任务是管理servlet的生命周期,而web容器更准确的说应该叫web服务器,它是来管理和部署web应用的.还有一种服务器叫 ...
- 【温故知新C/C++/opencv】取址符&||cv::groupRectangles||引用与值传递
cv::groupRectangles void groupRectangles(vector<Rect>& rectList, int groupThreshold, doubl ...
- Visual Studio Ultimate 2013 with Update 4
Visual Studio Ultimate 2013 with Update 4 是一个先进的开发解决方案,各种规模的团队通过它均可设计和创建引人注目的应用程序,使用户兴致勃勃. Visual St ...
- 解密SQL SERVER 2005加密存储过程,函数
在SQL SERVER 2005中必须用专用管理连接才可以查看过程过程中用到的表 EG:sqlcmd -A 1>use test 2>go 1>sp_decrypt 'p_testa ...
- mysql批量写入
MySQL批量写入语法是: INSERT INTO table (field1,field2,field3) VALUES (“a”,”b”,”c”), (“a1”,”b1”,”c1”),(“a2”, ...