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. 写入数据到Plist文件中时,第一次要创建一个空的数组,否则写入文件失败

    #pragma mark - 保存数据到本地Plist文件中 - (void)saveValidateCountWithDate:(NSString *)date count:(NSString *) ...

  2. While reading xxx.png pngcrush caught libpng error: Not a PNG file..

    While reading /XXX/XXX/XXX/img1.png pngcrush caught libpng error: Not a PNG filCould not find file: ...

  3. js完美解决IE6不支持position:fixed的bug

    详细内容请点击 <!DOCTYPE html><html><head><meta http-equiv="Content-Type" co ...

  4. miniui MVC datagrid数据绑定

    数据绑定 Default.cshtml <div id="datagrid1" class="mini-datagrid" style="wid ...

  5. JAVA之网页截屏

    先吐槽一下下: 表示接近两个月没有敲代码了,现在看一下代码都感觉有点生了.三天打鱼两天晒网是不行的,再说我本来就有“健忘症”,真的是木有办法啊 ̄へ ̄.我一直信奉一句话:“勤能补拙”,它也是我学习路上的 ...

  6. win7开启远程桌面

    1.启用windows防火墙 计算机管理----->服务----->Windows Firewall(双击进入,启动类型改为自动,点击应用,点击启动)2.启动gpedit.msc打开“本地 ...

  7. sql server 修改列类型

    如下代码中为修改bcp数据库中表B_TaskFileMonitor中的列FileSizeOriginal的类型为bigint use bcp; ); --判断是否存在这一列 IF COL_LENGTH ...

  8. transport

    #include<iostream> using namespace std; int transport(int a) { ; ) ; else a=a/; d=; ) { a=a*+; ...

  9. LLVM language 参考手册(译)(6)

    模块级内联汇编(Module-Level Inline Assembly) 模块包含“module-level inline assembly”块,这与GCC中的“file scope inline ...

  10. 试写Python内建函数range()

    还没查阅源码,先试着练手 class my_range(object): def __init__(self, *args): if not args: raise TypeError, 'range ...