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 ]
] 这道题接着上面那道题,是一个逆过程,只要稍微改一下就好了,因为上面一题螺旋式的遍历了整个数组,只要在遍历过程中将数字填进去就好了
 import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class Solution {
// List<Integer> result = new ArrayList<Integer>();
int result[][];
int count = 1;
public int[][] generateMatrix(int n) { result = new int[n][n];
if(0 == n)
return result;
spiralOrder(result);
return result;
} public void spiralOrder(int[][] matrix) { if(matrix.length == 1 && matrix[0].length == 1) //只有一个元素直接添加
{
matrix[0][0] = count++;
}
else if(matrix[0].length == 1){ //竖条
for(int i = 0; i < matrix.length; i++){
matrix[i][0] = count++;
}
}
else if(matrix.length == 1){ //横条
for(int i = 0; i < matrix[0].length; i++){
matrix[0][i] = count++;
}
}
else {
for(int i = 0; i < matrix[0].length; i++){ //添加第一排
matrix[0][i] = count++;
}
for(int i = 1; i < matrix.length; i++){ //添加最后一竖
matrix[i][matrix[0].length - 1] = count++;
}
for(int i = matrix[0].length - 2; i >= 0; i--){ //添加最后一排
matrix[matrix.length - 1][i] = count++;
}
for(int i = matrix.length - 2; i >= 1;i--){ //添加第一排
matrix[i][0] = count++;
}
if(matrix.length - 2 != 0 && matrix[0].length - 2 != 0){
int next[][] = new int[matrix.length - 2][matrix[0].length - 2];
spiralOrder(next); //递归求解下一个矩阵的值
for(int i = 1; i < matrix.length - 1; i++){
for(int j = 1; j < matrix[0].length - 1;j++){
matrix[i][j] = next[i - 1][j - 1];
}
} } }
}
}

Spiral Matrix II的更多相关文章

  1. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  2. 59. Spiral Matrix && Spiral Matrix II

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

  3. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  4. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  5. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  6. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  7. [LeetCode] Spiral Matrix II 螺旋矩阵之二

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

  8. 【leetcode】Spiral Matrix II (middle)

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

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

随机推荐

  1. BaseAdapter&ArrayAdapter在ListView中应用

    一:BaseAdapter:共同实现的基类的适配器,是ArrayAdapter SimpleAdapter等的父类, 一般用于比较复杂的ListView,扩展性强. 详细信息可查看谷歌官方API:ht ...

  2. 网络安全-PGP实现电子邮件加密

    PGP机制

  3. C#去除HTML标签(转)

    public static string ReplaceHtmlTag(string html, int length = 0) { string strText = System.Text.Regu ...

  4. Exchange之准备AD及域

    1.         若有旧版本的Exchange 2003,则需要执行以下命令: setup.com /PrepareLegacyExchangePermissions 2.         准备架 ...

  5. WPF DataGrid 操作列 类似 LinkButton

    WPF中没有类似LinkButton,所以只有运用Button及样式来实现LinkButton. DataGrid 操作列 实现 多个类似LinkButton按钮: 具体实现代码如下: <Dat ...

  6. python 中range与xrange的区别

    先来看看range与xrange的用法介绍 help(range)Help on built-in function range in module __builtin__: range(...) r ...

  7. 《APUE》第四章笔记(4)

    这算是在博客园写的第一篇文章啊,之前都在csdn写(虽然才写了几篇,因为开通也没多少天..),还是稍微期待下吧.我写博客的主要意图是一来能够记录下来自己所学过的东西,二来也想能够跟大家交流,能够得到更 ...

  8. mysql主从复制-linux版本

    来自:http://www.osyunwei.com/archives/7269.html,改版 mysql主从复制本文采用的是centos6.5+mysql-5.6.23版本之前在 windows7 ...

  9. “~/Views/Home/Text.aspx”处的视图必须派生自 ViewPage、ViewPage<TModel>、ViewUserControl 或 ViewUserControl<TModel>。

    在MVC架构中使用aspx页面,需要在Text.aspx中开头加入如下代码: <%@ Page Language="C#" Inherits="System.Web ...

  10. Git命令收集【不断更新中】

    git stash 可以用来保存暂时不想提交但又被修改过的文件. git stash pop 用来取出被保存在stash栈中的修改过的所有文件. git stash show 查询哪些文件被存放在了s ...