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平方的螺旋矩阵.例 ...
随机推荐
- fullfile
这个我总是忽略,见过也不少了,顺便写写,其实一些命令很方便的. 一个例子: root_dir = '../mcg/pre-trained'; addpath(root_dir); addpath(fu ...
- java面试题7
1.重载和重写的区别? 重载(Overload):(1)方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型.重载Overloading是一个类中多态性 ...
- UT源码+105032014070
设计三角形问题的程序 输入三个整数a.b.c,分别作为三角形的三条边,现通过程序判断由三条边构成的三角形的类型为等边三角形.等腰三角形.一般三角形(特殊的还有直角三角形),以及不构成三角形.(等腰直角 ...
- streamsets 包管理
streamsets 自带一个包管理,可以方便的进行三方组件的添加,比如我们需要处理mongodb 数据,默认是没有添加这个组件的,操作如下: 选择包管理 选择组件 安装 点击安装 提示界面 安装完成 ...
- graphql-yoga interface && union 使用
接口就是一个约定,方便数据的约定,union 可以实现数据类型的共享,减少代码量 基本项目 参考 https://github.com/rongfengliang/graphql-yoga-doc ...
- 机器学习 ----Tensorflow
机器学习笔记4-Tensorflow线性模型示例及TensorBoard的使用 机器学习笔记3-Tensorflow简介 机器学习笔记2 – sklearn之iris数据集 机器学习笔记1 - Hel ...
- noip2009最优贸易(水晶球)
题目:http://codevs.cn/problem/1173/ https://www.luogu.org/problemnew/show/P1073 本来考虑缩点什么的,后来发现不用. 只要记录 ...
- angularJS的ng-repeat-start
使用angularJS的同学对ng-repeat都不会陌生,他是用来进行数据循环的,一般用于数组或者对象.但是今天我们用到了一个ng-repeat-start. ng-repeat-start,与ng ...
- C# Async&Await
在async和await之前我们用Task来实现异步任务是这样做的: static Task<string> GetBaiduHtmlTAP() { //创建一个异步Task对象,内部封装 ...
- 设置vim颜色方案
获取所有vim颜色配置方案 ls /usr/share/vim/vim74/colors/ [root@lx ~]# ls /usr/share/vim/vim74/colors/ blue.vim ...