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 ]
]
 
与Spiral Matrix类似:
 
 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的更多相关文章

  1. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  2. 【leetcode】 Spiral Matrix

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  3. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

  4. 【leetcode】Spiral Matrix

    题目概要: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spi ...

  5. 【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 ...

  6. 【数组】Spiral Matrix II

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

  7. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. js获取iframe里的body内容

    做个页面 需要加入a.html 使用的js动态添加iframe 直接JQ添加的 代码 $(".banner-box").after(“<iframe src="ht ...

  2. ServletContext

    1.为什么需要servletContext    需求1 需求2 --------------->解决之道servletContext     servletContext 1.ServletC ...

  3. Mongodb的Samus驱动

    最近开始学习Mongodb方面的东西.. 看到有很多博主都说MongoDB的第三方驱动 Samus 对Linq的支持比较好..能够降低学习的成本..所以就想从这里开始.. 但是弊端在我学习了一半的时候 ...

  4. Orchard源码分析(4.4):Orchard.Caching.CacheModule类

    概述 CacheModule也是一个Autofac模块.   一.CacheModule类 CacheModule将DefaultCacheManager注册为ICacheManager:       ...

  5. [Angularjs]系列——学习与实践

    写在前面 这个系列是来这家公司到现在,边用边学,以及在工作中遇到的问题进行的总结.深入的东西不多,大多是实际开发中用到的东西. 这里做一个目录,方便查看. 系列文章 [Angularjs]ng-sel ...

  6. JSF的CommandXxx组件的update用法总结

    Update all forms 1 update="@(form)" Update first form 1 update="@(form:first)" U ...

  7. 安装使用ubuntu和opensuse

    liquid: n.液体, adj. 液体的, 流动的 liquidate: v. 清洗; 清算; 变现; 杀戮; weird: [wi2d] e:i, ir:2 离奇的,古怪的... 英文名称, 直 ...

  8. nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored

    修改nginx配置参数后,使用nginx -t检查配置. 提示successfull后就可以使用 nginx -s reload来重新加载配置 我配置的过程中遇到这样的问题,就是绑定了主机名后,重新加 ...

  9. shell编程中的select用法

    select 语句 select表达式是bash的一种扩展应用,擅长于交互式场合.用户可以从一组不同的值中进行选择: select var in ... ; do break; done .... n ...

  10. readline

    注意,向后表示向左,向前表示向右. "\C-f": forward-char 光标向右一个字符 "\C-b": backward-char  光标向左一个字符 ...