【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 ...
随机推荐
- PHP读取excel文档
PHP读取excel文档 项目需要读取Excel的内容,从百度搜索了下,主要有两个选择,第一个是PHPExcelReader,另外一个是PHPExcel. PHPExcelReader比较轻量级, ...
- eclipse如何导入java项目文件
平时下载到项目时,希望能够导入到eclipse中使用.但有些项目不能直接导入,需要做转换. 打开源文件目录,查询如下: 如果目录中包含pom.xml文件,则说明该项目由Maven构建的,参考以下 如何 ...
- H5图像遮罩-遁地龙卷风
(-1)写在前面 这个idea不是我的,向这位前辈致敬.我用的是chrome49.用到的图片资源在我的百度云盘里http://yun.baidu.com/share/link?shareid=1970 ...
- jsp+servlet
- Codeforces 260 A - A. Laptops
题目链接:http://codeforces.com/contest/456/problem/A 解题报告:有n种电脑,给出每台电脑的价格和质量,要你判断出有没有一种电脑的价格小于另一种电脑但质量却大 ...
- iOS 单元测试之XCTest详解(一)
iOS 单元测试之XCTest详解(一) http://blog.csdn.net/hello_hwc/article/details/46671053 原创blog,转载请注明出处 blog.csd ...
- eclipse pydev 跳转
[windows]-[Preference]-[Pydev]-[Interpreter-Python]-[Libraries]-system PYTHONPATH 全部加一遍,先加父目录,再加子目录.
- Linux搭建一个FTP服务器
1.安装vsftp 2.配置vsftpd.conf, vim /etc/vsftpd.conf 下面说说里面比较重要的选项 1 anonymous_enable=NO #不允许匿名用户 2 3 loc ...
- OpenCv haar+SVM训练的xml检测人头位置
注意:opencv-2.4.10 #include "stdio.h"#include "string.h"#include "iostream&qu ...
- phpDocumentor 注释语法详解
PHPDocumentor是强大的代码注释生成器,本文对各个参数进行了简单地的总结: @abstract-------------使用@abstract标记来声明一个方法,类变量或类必须重新定义子类中 ...