Leetcode#59 Spiral Matrix II
相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些。
注意当n是奇数的时候,中心小块要单独赋值(代码21行)
代码:
vector<vector<int> > generateMatrix(int n) {
vector<vector<int> > matrix(n, vector<int>(n, ));
int len = n - ;
int i = ;
int j = ;
int count = ;
while (len > ) {
for (int k = ; k < len; k++)
matrix[i][j++] = count++;
for (int k = ; k < len; k++)
matrix[i++][j] = count++;
for (int k = ; k < len; k++)
matrix[i][j--] = count++;
for (int k = ; k < len; k++)
matrix[i--][j] = count++;
i++;
j++;
len -= ;
}
if (len == )
matrix[i][j] = count;
return matrix;
}
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 (螺旋矩阵之二)
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 ...
随机推荐
- Silverlight 使用IsolatedStorage新建XML文件,并且用LINQ查询XML
代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-- ...
- hive到hbase的使用
一.简单介绍 hive的元数据保存在metastore里面,真实的数据一般位于hdfs中,可以通过hql来对数据进行分析.hbase中的数据也是存放在hdfs上的,可不可以使用hive来分析hbase ...
- js一些实用例子
1.获取焦点选中文本内容 $("#id").focus(function(){ this.select(); }); 2.表单提交方式 A.自动提交 setTimeout(func ...
- JS正则表达式获取字符串中特定字符
JS正则表达式获取字符串中得特定字符,通过replace的回调函数获取. 实现的效果:在字符串中abcdefgname='test'sddfhskshjsfsjdfps中获取name的值test 实 ...
- 直接拿来用!超实用的Java数组技巧攻略[转]
来自csdn http://www.csdn.net/article/2013-09-16/2816947-methods-for-java-arrays 本文分享了关于Java数组最顶级的11大方法 ...
- MSSQL 所有表中模糊查询
--搜索指定数据在那个对象中存在.txt(所有表中模糊查询) CREATE PROC sp_ValueSearch @value sql_variant, --要搜索的数据 @precision bi ...
- redis安装与php扩展
redis安装:就是一个解压缩的过程 注意先运行:redis-server.exe,在运行redis-cli.exe. php扩展redis. 以上是如何选择相应的文件. 在把文件放在php/ext下 ...
- 国际制造执行系统(MES)应用与发展
某些专家认为,当今制造业的生存三要素是信息技术(IT).供应链管理(SCM)和成批制造技术.使用信息技术就是由依赖人工的作业方式转变为作业的快速化.高效化,大量减少人工介入,降低生产经营成本:供应链管 ...
- Split字符串分割函数
非常非常常用的一个函数Split字符串分割函数. Dim myTest myTest = "aaa/bbb/ccc/ddd/eee/fff/ggg" Dim arrTest arr ...
- iOS中MVC设计模式
在组织大型项目的代码文件时,我们常用MVC的思想.MVC的概念讲起来非常简单,就和对象(object)一样.但是理解和应用起来却非常困难.今天我们就简单总结一下MVC设计理念. MVC(Model V ...