【leetcode】Spiral Matrix II (middle)
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 ]
]
思路:
我是直接套用了Spiral Matrix 的代码
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int>> ans(n, vector<int>(n,));
int num = ;
for(int i = ; i < (n + ) / ; i++)
{
//行 ----->
for(int j = i; j < n - i; j++)
ans[i][j] = num++;
//列 向下
for(int j = i + ; j < n - i; j++)
ans[j][n - - i] = num++;
//行 <-----------
if(n - i - <= i) //列号 一定要比向左时的列号小 防止重复
break;
for(int j = n - i - ; j >= i; j--)
ans[n - i - ][j] = num++;
//列 向上
if(i >= n - - i) //行号 一定要比向下时的行号大 防止重复
break;
for(int j = n - i - ; j >= i + ; j--)
ans[j][i] = num++;
}
return ans;
}
};
另一种写法的:
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > ret( n, vector<int>(n) );
int k = , i = ;
while( k <= n * n )
{
int j = i;
// four steps
while( j < n - i ) // 1. horizonal, left to right
ret[i][j++] = k++;
j = i + ;
while( j < n - i ) // 2. vertical, top to bottom
ret[j++][n-i-] = k++;
j = n - i - ;
while( j > i ) // 3. horizonal, right to left
ret[n-i-][j--] = k++;
j = n - i - ;
while( j > i ) // 4. vertical, bottom to top
ret[j--][i] = k++;
i++; // next loop
}
return ret;
}
};
【leetcode】Spiral Matrix II (middle)的更多相关文章
- 【leetcode】Set Matrix Zeroes(middle)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...
- 【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】Number of Islands(middle)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- 【leetcode】Combination Sum III(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【leetcode】 search Insert Position(middle)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- webapp开发要点记录
1. iphone 各机型 机型 分辨率 像素比 物理分辨率 高* 宽 * 后 主屏对角线长度 重量 像素密度(ppi) iphone4/iphone4s 320 * 480 2 640 * 960 ...
- nyoj 4 779 兰州烧饼
兰州烧饼 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 烧饼有两面,要做好一个兰州烧饼,要两面都弄热.当然,一次只能弄一个的话,效率就太低了.有这么一个大平底锅,一 ...
- 学习javascript系列之变量
在javascript全局变量中,未加var声明的全局变量和加上var声明的全局变量是不同的,虽然都是window对象的属性. ; window.a //1 delete a //false; 通过v ...
- C语言中可变参数函数实现原理
C函数调用的栈结构 可变参数函数的实现与函数调用的栈结构密切相关,正常情况下C的函数参数入栈规则为__stdcall, 它是从右到左的,即函数中的最右边的参数最先入栈.例如,对于函数: void fu ...
- js兼容注意事项--仅供参考
做BS开发就难免会用到javascript,而每个浏览器对javascript的支持有不同.这就需要我们程序员去兼容他们,不然有些浏览器就无法运行我们的代码.就会造来客户的投诉,如果让BoSS知道了, ...
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
这个图片异步加载并缓存的类已经被很多开发者所使用,是最常用的几个开源库之一,主流的应用,随便反编译几个火的项目,都可以见到它的身影. 可是有的人并不知道如何去使用这库如何进行配置,网上查到的信息对于刚 ...
- UOJ30——【CF Round #278】Tourists
1.感谢taorunz老师 2.题目大意:就是给个带权无向图,然后有两种操作, 1是修改某个点的权值 2是询问,询问一个值,就是u到v之间经过点权的最小值(不可以经过重复的点) 操作数,点数,边数都不 ...
- C++ 复制构造函数
C++类的设计中,如果某些函数没有显式定义,C++会自动生成,复制构造函数便是其中之一,其他的还有默认构造函数.赋值操作符.默认析构函数.地址操作符.一个类的复制构造函数的原型一般为: Class_n ...
- phpcms 根据条件调取内容
asdasdsd <script> 正则 截取 function getUrlParam(name) { var reg = new RegExp("(^|&)" ...
- VirtualBox centos 6.5 minimal 开启网络
默认情况下载的centos 6.5 minimal是不开启网卡功能的,按照下面的步骤开启网卡. vi /etc/sysconfig/network-script/ifcfg-eth0 将其中的 ONB ...