【leetcode】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 ]
]
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
int x1=;
int y1=;
int x2=n-;
int y2=n-;
int count=;
vector<vector<int>> result(n,vector<int>(n));
while(count<n*n)
{
for(int j=y1;j<=y2;j++)
{
count++;
result[x1][j]=count;
}
for(int i=x1+;i<=x2;i++)
{
count++;
result[i][y2]=count;
}
for(int j=y2-;j>=y1;j--)
{
count++;
result[x2][j]=count;
}
for(int i=x2-;i>x1;i--)
{
count++;
result[i][x1]=count;
}
x1++;y1++;x2--;y2--;
}
return result;
}
};
【leetcode】Spiral Matrix II的更多相关文章
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【leetcode】 Spiral Matrix
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
- 【leetcode】Spiral Matrix
题目概要: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spi ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【数组】Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 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 ...
- [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 ...
随机推荐
- OC-Q&A
How to declare a string in Objective-C ? A C string is just like in C. char myCString[] = "test ...
- 在CentOS上搭建apache和PHP服务器环境(转)
1.您也可以使用一键自动部署环境的工具,请参见网友开发的这个工具 http://www.centos.bz/2013/08/ezhttp-tutorial/ 2. 安装: wget -c http:/ ...
- Orchard源码分析(7.1):Routing(路由)相关
概述 关于ASP.NET MVC中路由有两个基本核心作用,一是通过Http请求中的Url参数等信息获取路由数据(RouteData),路由数据包含了area.controller.action的名称等 ...
- android控件库(2)-仿Google Camera 的对焦效果
一直很喜欢Google Camera的自动对焦效果,今日闲来无事,自己做了个: 废话不多说,代码才是王道: package com.example.test.view; import com.exam ...
- [转]12款最佳Linux命令行终端工具
摘要 “工欲善其事必先利其器”,作为菜鸟,也是从别人那里偷学来的一些东东.今天看到同事用到一个终端命令行工具,觉得自己弱爆了.然后在网上搜了下该工具.发现类似的工具还是挺多的,只是自己不知道罢了. 原 ...
- [转]Ubuntu 16.04建议安装
Ubuntu 16.04发布了,带来了很多新特性,同样也依然带着很多不习惯的东西,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java ...
- 我也来写:数据库访问类DBHelper(转)
一.前言 相信许多人都百度过:“.net 数据库访问类”.然后就出来一大堆SqlHelper.我也用过这些SqlHelper,也自己写过,一堆静态方法,开始使用起来感觉很不错,它们也确实在很多时候可以 ...
- oracle中的连接查询与合并查询总结
连接查询: 连接查询是指基于多张表或视图的查询.使用连接查询时,应指定有效的查询条件,不然可能会导致生成笛卡尔积.如现有部门表dept,员工表emp,以下查询因查询条件无效,而产生笛卡尔积: (各 ...
- NHibernate配置
因为NHibernate被设计为可以在许多不同环境下工作,所以它有很多配置参数.幸运的是,大部分都已经有默认值了. NHibernate.Test.dll包含了一个示例的配置文件app.config, ...
- PHP文件操作 读取与写入
基本知识: PHP文件系统是基于Unix系统的 文件数据基本类型:二进制数据.文本数据 文件输入流:数据从源文件到内存的流动 文件输出流:数据从内存保存到文件的流动 文件操作函数: >>& ...