【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 ...
随机推荐
- spring 容器技术入门
官方文档 翻译 https://waylau.gitbooks.io/spring-framework-4-reference/content/III.%20Core%20Technologies/C ...
- Java多线程编程核心技术---单例模式与多线程
立即加载/饿汉模式 立即加载就是使用类的时候已经将对象创建完毕. public class MyObject { //立即加载方式==饿汉模式 private static MyObject myOb ...
- C# 正则匹配domain
1.带协议表达式 var pattern = @"[(?<=http://)|(?<=https://)]+[\w\.]+[^/?#]"; 2.不带协议表达式 var ...
- 关于在windows上的wamp集成环境和xampp上安装mongo扩展
今天来学习下mongodb,在装PHP扩展的时候本来是一个很轻松的事情,结果并不是我想想的那么简单. 我的集成环境是xampp的php版本是5.6的x86.我开启了安全模式,所以我需要mongo时ts ...
- 2015年12月01日 GitHub入门学习(一)GitHub简介
序:Github理念是Social Coding(社会化编程).octocat是它的吉祥物. 一.Github与Git的区别与联系 区别:GIT是仓库,Github是提供一种将代码提交到Git仓库的服 ...
- TCP的流模式与UDP的报文模式对比
1 案例背景 在学习TCP-IP协议详解卷一时,读到介绍TCP协议的部分,发现TCP的首部是没有报文总长度字段的,而在UDP中是有的,对这个问题的思考引出了两者之间的区别. 2 案例 ...
- putchar和puts
#include<stdio.h> int main() { char a = 'h'; char b[] = "hello"; putchar(a); //putch ...
- UML之用例图
用例图概要 ²用例图是被称为参与者的外部用户所能观察到的系统功能的模型图. (<UML参考手册>) ²用例图列出系统中的用例和系统外的参与者,并显示哪个参与者参与了哪个用例的执行 (或称为 ...
- CSS样式案例(2)-制作一个简单的登录界面
首先来张完工的效果图. 一.html文件如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...
- CentOS6.3编译安装Memcached集群分布式缓存代理Magent-0.6出错汇总
参考文章:Memcached集群/分布式/高可用 及 Magent缓存代理搭建过程 详解,搭建Magent,在编译的过程中会出现很多错误: #编译安装安装magent到 /usr/local/mage ...