【一天一道LeetCode】#59. Spiral Matrix II
一天一道LeetCode系列
(一)题目
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 ]
]
(二)解题
思路参考: 【一天一道LeetCode】#54. Spiral Matrix
还是一样按圈赋值,每一圈的起点分别为(0,0),(1,1)…..
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> ret;
if(n==0) return ret;
for(int i = 0 ; i < n ; i++)
{
vector<int> tmp(n,0);
ret.push_back(tmp);
}
int count = 0 ;
int px = 0 , py = 0;//初始值
int start = 0;//每一圈的起点
while(start<n/2)
{
int i;
for(i = py ; i<n - start; i++) {//从左往右
ret[px][i] = ++count;
}
py = i-1;
for(i = px+1 ; i<n - start ; i++){//从上往下
ret[i][py] = ++count;
}
px = i-1;
for(i = py-1 ; i>=start ; i--){//从右往左
ret[px][i] = ++count;
}
py = i+1;
for(i = px-1 ; i>=start + 1 ; i--){//从下往上
ret[i][py] = ++count;
}
start++;
px = py = start;//下一圈
}
if(n%2==1) ret[px][py] =++count;//n为奇数的时候需要考虑中心值
return ret;
}
};
【一天一道LeetCode】#59. Spiral Matrix II的更多相关文章
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- LeetCode 59. Spiral Matrix II (螺旋矩阵之二)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [leetcode]59. Spiral Matrix II螺旋遍历矩阵2
Given a positive integer n, generate a square matrix filled with elements from 1 to n^2 in spiral or ...
- 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】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
- ionic tab页面跳转
要使用到Tabs,现将Tabs导入 import { NavController, ModalController, Tabs } from 'ionic-angular'; 把要选择的Tabs页面的 ...
- Node.js ZLIB
Zlib 稳定性: 3 - 文档 可以通过以下方式访问这个模块: var zlib = require('zlib'); 这个模块提供了对 Gzip/Gunzip, Deflate/Inflate, ...
- JVM的Server与Client运行模式区别与切换
概述 JVM有两种运行模式Server与Client.两种模式的区别在于,Client模式启动速度较快,Server模式启动较慢:但是启动进入稳定期长期运行之后Server模式的程序运行速度比Clie ...
- NLP系列(5)_从朴素贝叶斯到N-gram语言模型
作者: 龙心尘 && 寒小阳 时间:2016年2月. 出处: http://blog.csdn.net/longxinchen_ml/article/details/50646528 ...
- Zookeeper的功能以及工作原理 (转自:http://www.cnblogs.com/felixzh/p/5869212.html)
1.ZooKeeper是什么?ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,它是集群的管理者,监视着集群中各个节点的状态根据节点提交的 ...
- Goaccess解析nginx日志备忘
参考 http://nginx.org/en/docs/http/ngx_http_log_module.html?&_ga=1.92028562.949762386.1481787781#l ...
- Objective-C基础之简析深浅copy
一.从面向对象到Objective-C概览copy 1.面向对象: In object-oriented programming, object copying is creating a copy ...
- Java学习之二维数组定义与内存分配详解
二维数组:就是元素为一维数组的一个数组. 格式1: 数据类型[][] 数组名 = new 数据类型[m][n]; m:表示这个二维数组有多少个一维数组. n:表示每一个一维数组的元素有多少个. 注意: ...
- Struts 2 之拦截器
拦截器概述 Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP(Aspect Oriented Progr ...
- 剑指Offer——乐视笔试题+知识点总结
剑指Offer--乐视笔试题+知识点总结 情景回顾 时间:2016.9.19 15:10-17:10 地点:山东省网络环境智能计算技术重点实验室 事件:乐视笔试 总体来说,乐视笔试内容体量不算少, ...