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. html+css--水平居中总结-不定宽块状元素方法(三)

    来源:http://www.imooc.com/code/6365 除了前两节讲到的插入table标签,以及改变元素的display类型,可以使不定宽块状元素水平居中之外,本节介绍第3种实现这种效果的 ...

  2. 【CSS3】---text-overflow 与 word-wrap

    text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 语法: 但是text-overflow只是用来说明文字溢出时用什么方式显示,要实现溢出时产生省略号的效果,还须定 ...

  3. 从ASP.NET的web1子界面刷新打开web1的web0父界面

    单击web0界面的按钮bt1触发一下代码: protected void btnSave_Click(object sender, EventArgs e) { string parentJs = @ ...

  4. Javascript 日期时间超强正则表达式

    var reg = /^([0-9]{4})-((?:0[1-9]|[1-9]|1[1-2]))-((?:(?:0[1-9]|[1-9])|1[0-9]|2[0-9]|3[0-1]))$|^([0-9 ...

  5. Python中lambda表达式学习

    lambda只是一个表达式,函数体比def简单很多. lambda的主体是一个表达式,而不是一个代码块.仅仅能在lambda表达式中封装有限的逻辑进去. lambda表达式是起到一个函数速写的作用.允 ...

  6. WCF之Host宿主

    Self_hosting自托管宿主. 过程:手动创建Host实例,把服务端点添加到Host实例上,把服务接口与Host关联. 一个Host只能指定一个服务类型,但是可以添加多个服务端点,也可以打开多个 ...

  7. java静态代码块、初始化块和构造方法的执行顺序

    分析:当执行new Child()时,它首先去看父类里面有没有静态代码块,如果有,它先去执行父类里面静态代码块里面的内容,当父类的静态代码块里面的内容执行完毕之后,接着去执行子类(自己这个类)里面的静 ...

  8. [NOIP2014]解方程

    3732 解方程  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 输入描述 Input Descrip ...

  9. linux 线程函数大全

    Technorati 标签: Linux thread 索引: 1.创建线程pthread_create 2.等待线程结束pthread_join 3.分离线程pthread_detach 4.创建线 ...

  10. int * const 与 const int * 的区别

    type * const 与 const type * 是在C/C++编程中特别容易混淆的两个知识点,现在就以 int * const 和 const int * 为例来简略介绍一下这两者之间的区别. ...