59. Spiral Matrix II (Array)
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)的更多相关文章
- 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 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- 59. Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 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 ...
- 59. Spiral Matrix II(中等,同54题)
Given an integer \(n\), generate a square matrix filled with elements from 1 to \(n^2\) in spiral or ...
- 【一天一道LeetCode】#59. Spiral Matrix II
一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
随机推荐
- java 生成xml文件
这里也使用的是import org.w3c.dom.Document; 首先创建document对象,给该对象赋值,然后将document对象使用transformer的transformer转换方法 ...
- Java中的HashMap的工作原理是什么?
问答题23 /120 Java中的HashMap的工作原理是什么? 参考答案 Java中的HashMap是以键值对(key-value)的形式存储元素的.HashMap需要一个hash函数,它使用ha ...
- L188
This is the view from the instrument deployment camera of InSight, America’s latest probe to Mars, w ...
- New Concept English Two 28 76
$课文74 舞台之外 784. An ancient bus stopped by a dry river bed and a party of famous actors and actresse ...
- Winform开发之DataGridView事件和属性
DataDridView的事件和属性非常多,一一介绍还是不现实,这里借鉴一下园友和MSDN上的介绍吧 1.C#中 DataGridView 属性说明(转载) 2.MSDN上DataGridView事件 ...
- sourcetree回退到历史节点
1. 原理 原理,我们都知道Git是基于Git树进行管理的,要想要回滚必须做到如下2点: 本地头节点与远端头节点一样(Git提交代码的前提条件):于本地头节点获取某次历史节点的更改.说的有点抽象,以图 ...
- oracle Union 中 ORA-12704:字符集不匹配问题的解决 .
在使用Union all连接时,若A集合中某列为nvarchar2或nvarchar类型,而B集合中无此列,用‘ ’ 来代替是会报字符集不匹配,解决方法有两种,见下面的示例 例: select '中国 ...
- Http消息头中常用的请求头和响应头
作为Web开发对常用http的请求头和响应头熟悉了解一下还是很有必要的.比如请求头中Content-type指定了请求的内容,若类型是application/x-www-form-urlencoded ...
- centos 6.5 下 安装 git
2018-07-02 centos安装Git有两种方式,一种通过源安装,通过如下命今,即可一键安装成功: yum -y install git 但是通过源安装的Git,目前最高版本是1.7,想要使用更 ...
- 黄聪:VPS服务器如何配置PHP.ini解决wordpress使用WP-Mail-SMTP插件发邮件出现Could not connect to SMTP host的解决办法
1.首先是WP-Mail-SMTP的下载地址:http://wordpress.org/plugins/wp-mail-smtp/ 2.出现Could not connect to SMTP host ...