【LeetCode】59. Spiral Matrix II
Spiral Matrix II
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实现基本一致,只不过上题是遍历输出,这题是遍历输入。
由于这题保证是方阵,因此不用考虑Spiral Matrix中重复扫描行/列的问题。
class Solution { public:vector<vector<int> > generateMatrix(int n) {int layer = (n+1)/2;vector<vector<int> > ret(n, vector<int>(n, 0));int num = 1;for(int i = 0; i < layer; i ++){//top-left --> top-rightfor(int j = i; j < n-i; j ++)ret[i][j] = num ++;//top-right --> bottom-rightfor(int j = i+1; j < n-i; j ++)ret[j][n-1-i] = num ++;//bottom-right --> bottom-leftfor(int j = n-1-i-1; j >= i; j --)ret[n-1-i][j] = num ++;//bottom-left --> top-leftfor(int j = n-1-i-1; j > i; j --)ret[j][i] = num ++;}return ret;} };
转载于:https://www.cnblogs.com/ganganloveu/p/4157415.html
【LeetCode】59. Spiral Matrix II相关推荐
- LeetCode 59. Spiral Matrix II
59. Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- 59. Spiral Matrix II
/** 59. Spiral Matrix II * 12.5 by Mingyang* 注意,这里我们说的Matrix就是正方形,不再是长方形了,所以我们会用* 更简单的方法,就是直接上下左右分别加 ...
- 【leetcode】132. Palindrome Partitioning II
题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...
- LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...
- 【LeetCode】House Robber I II 解题报告
[题目] I You are a professional robber planning to rob houses along a street. Each house has a certain ...
- 【leetcode】Jump Game I, II 跳跃游戏一和二
题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at the first ...
- 【leetcode】324.摆动排序 II (四种解法,快速排序+3way-partition等,java实现)
324. 摆动排序 II 难度中等 给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序. 示例 1 ...
- 【LeetCode】1140-石子游戏II
爱丽丝和鲍勃继续他们的石子游戏.许多堆石子 排成一行,每堆都有正整数颗石子 piles[i].游戏以谁手中的石子最多来决出胜负. 爱丽丝和鲍勃轮流进行,爱丽丝先开始.最初,M = 1. 在每个玩家的回 ...
- 【leetcode】45. Jump Game II 非负数组的最少跳跃步数
1. 题目 Given an array of non-negative integers, you are initially positioned at the first index of th ...
- 59. Spiral Matrix II ***
description: 螺旋型填充矩阵 Note: Example: Example:Input: 3 Output: [[ 1, 2, 3 ],[ 8, 9, 4 ],[ 7, 6, 5 ] ] ...
最新文章
- c实现面向对象编程(3)
- 后端返回number类型数据_【JavaScript 教程】标准库—Number 对象
- 神经网络中的权重初始化常用方法
- DS博客作业05--树
- NSTimer 增加引用计数, 导致内存泄露,
- POJ3686 The Windy's 【费用流】*
- c# 文件流读写文件
- 树莓派也跑Docker和.NET Core
- 实战:基于 Spring 的应用配置如何迁移至阿里云应用配置管理 ACM
- thinkjs 学习笔记
- 游戏开发之类的初始化成员列表和委托构造函数(C++基础)
- LNMPS PHP 团队开发 需要用到的相关工具(2017年11月更新)
- Linux内核那些事之连接跟踪
- operators库
- PE头之IMAGE_OPTIONAL_HEADER解析
- 服务器上的服务一直自动关闭,服务器会话连接自动关闭怎么办?
- 电信计费系列1-座机计费
- 微信小程序内含H5页面实现方式
- 计算机软件被删了,电脑Word软件被卸载了,怎么办?
- 服务计算 -- 搭建私有云
热门文章
- hdu1599+floyd最小环
- [Node.js] BDD和Mocha框架
- Django模板-模板标签
- vue脚手架项目中引入jquery
- 小编带着小白看springboot源码3
- 图片字符串base64的判断
- Cisco IOS Rootkit工具该怎么写
- [20171120]11G关闭直接路径读.txt
- 自己学Docker:4.開始了解Docker的工作模式
- warning: mysql-community-libs-5.7.11-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5