【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 ...
随机推荐
- JS/HTML 保存图片到本地:HTML <a> download 属性
JS如何保存图片到本地呢?自己百度一下吧! 这里想要说的是,可以利用 HTML 的 <a> 标签 来是实现保存图片到本地的功能,参考代码如下: <a href="http: ...
- img标签src=""和background-image:url();引发两次请求页面bug
img标签src=""和background-image:url();引发两次请求页面bug 具体原因是,在img 对象的src 属性是空字符串("")的时 ...
- C++公有派生
C++中的公有派生: 1.格式:Class A:public B{...}: 例如我们先声明了一个基类:SafeIntArray,当我们要声明它的派生类IndexIntArray时,格式如下:
- [POJ2586]Y2K Accounting Bug
[POJ2586]Y2K Accounting Bug 试题描述 Accounting for Computer Machinists (ACM) has sufferred from the Y2K ...
- ios中二维码的使用之二: 二维码的扫描
二维码的扫描: 1,导入支持框架,<AVFoundation/AVFoundation.h> 2 ,扫描:
- [转载]JavaEE学习篇之——JQuery技术详解
原文链接:http://blog.csdn.net/jiangwei0910410003/article/details/32102187 1.简介2.工具3.jQuery对象 1.DOM对象转化成j ...
- php源码安全加密之PHP混淆算法.
php源码安全加密的前世今生,本想发在教程区中.不知道怎么发,就写在这里面吧.PHP加密,解密是一直的话题,本人菜鸟,今天就简单向大家介绍一下并说说其中原理.提供一些加密的混淆算法.一\PHP的加密总 ...
- 2016年10月10日--穷举、迭代、while循环
穷举 将所有可能性全部全部走一遍,使用IF筛选出满足的情况 练习: 1.单位给发了一张150元购物卡, 拿着到超市买三类洗化用品. 洗发水15元,香皂2元,牙刷5元. 求刚好花完150元,有多少种买法 ...
- 5.3(2)----机器人走方格2(CC150)
这道题只需要把障碍点都设为0就可以了. public static int countWays(int[][] map,int x, int y){ if( x < 0 || y < 0) ...
- ssh authentication魔鬼细节--.ssh文件夹权限
换到7后出现莫名奇妙问题,ssh验证始终不起作用. 服务器 centos7#mkdir ~/.ssh centos7#touch ~/.ssh/authorized_keys centos7#chmo ...