给出正整数 n,生成正方形矩阵,矩阵元素为 1 到 n2 ,元素按顺时针顺序螺旋排列。
例如,
给定正整数 n = 3,
应返回如下矩阵:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]
详见:https://leetcode.com/problems/spiral-matrix-ii/description/

Java实现:

class Solution {
public int[][] generateMatrix(int n) {
int[][] res=new int[n][n];
if(n==0){
return res;
}
int top=0;
int bottom=n-1;
int left=0;
int right=n-1;
int num=1;
while(top<=bottom&&left<=right){
if(num<=n*n){
for(int j=left;j<=right;++j){
res[top][j]=num;
++num;
}
}
++top;
if(num<=n*n){
for(int i=top;i<=bottom;++i){
res[i][right]=num;
++num;
}
}
--right;
if(num<=n*n){
for(int j=right;j>=left;--j){
res[bottom][j]=num;
++num;
}
}
--bottom;
if(num<n*n){
for(int i=bottom;i>=top;--i){
res[i][left]=num;
++num;
}
}
++left;
}
return res;
}
}

059 Spiral Matrix II 旋转打印矩阵 II的更多相关文章

  1. 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

    [059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...

  2. 059. Spiral Matrix II

    题目链接:https://leetcode.com/problems/spiral-matrix-ii/description/ Given a positive integer n, generat ...

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

  4. 054 Spiral Matrix 旋转打印矩阵

    给出一个 m x n 的矩阵(m 行, n 列),请按照顺时针螺旋顺序返回元素.例如,给出以下矩阵:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]应该返回 [1,2, ...

  5. 【LeetCode每天一题】Spiral Matrix II(螺旋数组II)

    Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...

  6. 【leetcode刷题笔记】Spiral Matrix II

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

  7. [Leetcode] spiral matrix ii 螺旋矩阵

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

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

  9. C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...

随机推荐

  1. B1/B2签证的有效期——对于B1/B2签证,停留期最长不超过183天

    一.关于签证有效期首先我们要知道当我们历经困难从签证官手里拿到自己的签证的时候,签证上面有个时间这个我们叫做签证有效期.B1/B2一般是一年多次往返的,这个只跟你申请的签证类型有关,与你填表的时候写的 ...

  2. homebrew cask安装launch rocket【转】

    简介 brew cask是一个用命令行管理Mac下应用的工具,它是基于homebrew的一个增强工具. homebrew可以管理Mac下的命令行工具,例如imagemagick, nodejs,如下所 ...

  3. poj1966Cable TV Network——无向图最小割(最大流)

    题目:http://poj.org/problem?id=1966 把一个点拆成入点和出点,之间连一条边权为1的边,跑最大流即最小割: 原始的边权赋成inf防割: 枚举源点和汇点,直接相邻的两个点不必 ...

  4. APNS消息推送实现

    转自:http://blog.csdn.net/biaobiaoqi/article/details/8058503 一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1 ...

  5. WPF中数据绑定问题

    在数据库中字段不区分大小写,可以页面是区分的,一开始以为不区分,可我从数据库查出了数据在前台就是不显示想了半天才发现的. <sdk:DataGrid FrozenColumnCount =&qu ...

  6. php + mssql乱码

    当用PHP自带的模块php_mssql.dll去调用MSSQL数据库时,中文数据会乱码.但如果我们采用ADODB的方式去做,就不会乱码了.请看下面的具体实例: 调用开源的adodb.inc.php(支 ...

  7. python的logging模块详细使用demo

    import logging import os from logging import handlers from datetime import datetime class MyLog(): d ...

  8. C# 清除cookies

    不同的浏览器会把cookie文件保存在不同的地方 以下是C# WebBrowser控件cookies的存放路径 C:\Users\{你的帐号名}\AppData\Local\Microsoft\Win ...

  9. In-App Purchase Programming Guide----(八) ---- Preparing for App Review

    Preparing for App Review After you finish testing, you’re ready to submit your app for review. This ...

  10. 【机器学习】决策树C4.5、ID3

    一.算法流程 step1:计算信息熵 step2: 划分数据集 step3: 创建决策树 step4: 利用决策树分类 二.信息熵Entropy.信息增益Gain 重点:选择一个属性进行分支.注意信息 ...