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 ]
]

解题思路:

参考Java for LeetCode 054 Spiral Matrix,修改下代码即可,JAVA实现如下:

  1. static public int[][] generateMatrix(int n) {
  2. if(n==0)
  3. return new int[0][0];
  4. int[][] matrix=new int[n][n];
  5. int num=0,left = 0, right = matrix[0].length - 1, up = 0, down = matrix.length - 1;
  6. while (left <= right && up <= down) {
  7. for (int i = left; i <= right&&up<=down; i++)
  8. matrix[up][i]=++num;
  9. up++;
  10. for (int i = up; i <= down&&left<=right; i++)
  11. matrix[i][right]=++num;
  12. right--;
  13. for (int i = right; i >= left&&up<=down; i--)
  14. matrix[down][i]=++num;
  15. down--;
  16. for (int i = down; i >= up&&left<=right; i--)
  17. matrix[i][left]=++num;
  18. left++;
  19. }
  20. return matrix;
  21. }

Java for LeetCode 059 Spiral Matrix II的更多相关文章

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

  2. 【leetcode】Spiral Matrix II

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

  3. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  4. 059. Spiral Matrix II

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

  5. 【leetcode】Spiral Matrix II (middle)

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

  6. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

  7. LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

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

  8. Java for LeetCode 054 Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. LeetCode 58 Spiral Matrix II

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

随机推荐

  1. yii授权

    ACF (访问控制过滤器) 在你控制器的添加下列的 行为 方法 use yii\filters\AccessControl; class DefaultController extends Contr ...

  2. opencv笔记4:模板运算和常见滤波操作

    time:2015年10月04日 星期日 00时00分27秒 # opencv笔记4:模板运算和常见滤波操作 这一篇主要是学习模板运算,了解各种模板运算的运算过程和分类,理论方面主要参考<图像工 ...

  3. BZOJ-3524 Couriers 可持久化线段树

    可持久化线段树,其实就是类主席树了.. 3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 128 MB Submit: 1124 Sol ...

  4. SQL Server中,Numric,Decimal,Money三种字段类型的区别

    都是精确数据类型, 前两个可以自己定义长度和小数位数, Money的定义相当于Numric(19,4) numeric(10,2) 表示最大可以放10位数,但这10位数里有2位是小数如: 123456 ...

  5. SPOJ Play on Words

    传送门 WORDS1 - Play on Words #graph-theory #euler-circuit Some of the secret doors contain a very inte ...

  6. UVA 1626 Brackets sequence(括号匹配 + 区间DP)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/E 题意:添加最少的括号,让每个括号都能匹配并输出 分析:dp ...

  7. jQuery EasyUI datagrid实现本地分页的方法

    http://www.codeweblog.com/jquery-easyui-datagrid%e5%ae%9e%e7%8e%b0%e6%9c%ac%e5%9c%b0%e5%88%86%e9%a1% ...

  8. c3p0配置详解

    数据库连接池C3P0框架是个非常优异的开源jar,高性能的管理着数据源,这里只讨论程序本身负责数据源,不讨论容器管理. 一.实现方式: C3P0有三种方式实现: 1.自己动手写代码,实现数据源 例如: ...

  9. pthread_rwlock

    读写锁   1.概述 读写锁与互斥量类似,不过读写锁允许更高的并行性.互斥量要么是锁住状态,要么是不加锁状态,而且一次只有一个线程对其加锁.读写锁可以有三种状态:读模式下加锁状态,写模式下加锁状态,不 ...

  10. AppSettingManager

    public class AppSettingManager { public static bool Update(string key, string value) { try { var con ...