【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 ...
随机推荐
- 通过LVS+Keepalived搭建高可用的负载均衡集群系统
1. 安装LVS软件 (1)安装前准备操作系统:统一采用Centos6.5版本,地址规划如下: 服务器名 IP地址 网关 虚拟设备名 虚拟ip Director Server 192.168 ...
- ServletContext
1.为什么需要servletContext 需求1 需求2 --------------->解决之道servletContext servletContext 1.ServletC ...
- php self与static的区别
self vs static 用一个demo来直接说明self与static的区别.self示例: <?phpclass Vehicle { protected static $name ...
- Orchard源码分析(7):ASP.NET MVC相关
概述 Orchard归根结底是一个ASP.NET MVC(以后都简称为MVC)应用,但在前面的分析中,与MVC相关内容的涉及得很少.MVC提供了非常多的扩展点,本文主要关注Orchard所做的扩展.主 ...
- python 日志收集系统
服务器端: #!/usr/bin/env python # -*- coding:utf-8 -*- import socket ip_port = ('0.0.0.0',9999) sk = soc ...
- [Html5]sessionStorage和localStorage的区别
摘要 有时需要在浏览器中保存一些数据,特别在app中嵌入的h5页面中,需要在webview中保存一些数据,作为客户端的数据持久化. h5中web storage有两种存储方式:sessionStora ...
- php-fpm服务启动脚本
在php-fpm还是打补丁的时候,php-fpm重启只需要执行php-fpm restart或者reload, 自从php5.3之后,php-fpm的启动和停止显得比较麻烦,特意改写了一份nginx的 ...
- Emacs教程
中文 http://www.cnblogs.com/robertzml/category/209299.html 英文 http://ergoemacs.org/emacs/emacs_fun.htm ...
- KVM虚拟机内存不足,调整参数
Dec :: vgfs001 kernel: tiotest_AMD_x86 invoked oom-killer: gfp_mask=, oom_adj=, oom_score_adj= Dec : ...
- 基于iSCSI的SQL Server 2012群集测试(二)--SQL群集安装后初始化配置测试
4.群集安装后初始化配置测试 4.1 禁用full-text 服务和Browser服务 Full-text服务:公司目前暂不使用,需在两个节点上分别禁用 Browser服务:为保证安全,建议将Brow ...