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

Example:

Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
class Solution {
public int[][] generateMatrix(int n) {
int[][] matrix = new int[n][n];
int beginRow = 0, endRow = n - 1;
int beginCol = 0, endCol = n - 1;
int num = 1;
while(beginRow <= endRow && beginCol <= endCol) {
for(int i = beginCol; i <= endCol; i++) {
matrix[beginRow][i] = num;
num += 1;
}
beginRow += 1; for (int i = beginRow; i <= endRow; i++) {
matrix[i][endCol] = num;
num += 1;
}
endCol -= 1;
// no need to check boarder b/c it is square
for (int i = endCol; i >= beginCol; i--) {
matrix[endRow][i] = num;
num += 1;
}
endRow -= 1; for (int i = endRow; i >= beginRow; i--) {
matrix[i][beginCol] = num;
num += 1;
}
beginCol += 1;
}
return matrix;
}
}
												

[LC] 59. Spiral Matrix II的更多相关文章

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

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

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

  3. 【leetcode】59.Spiral Matrix II

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

  4. Leetcode#59 Spiral Matrix II

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

  5. 59. Spiral Matrix II

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

  6. LeetCode OJ 59. Spiral Matrix II

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

  7. 59. Spiral Matrix II(中等,同54题)

    Given an integer \(n\), generate a square matrix filled with elements from 1 to \(n^2\) in spiral or ...

  8. 【一天一道LeetCode】#59. Spiral Matrix II

    一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

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

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

随机推荐

  1. lemon

    这本是一个技术博客网站 我却用来记录关于六月 就好像特别才不配你的特别一样 就好像以后要特别喜欢cos一样 就好像再特别的六月也会过渡到七月一样

  2. javaweb03 javaservlet基础一

    1.使用JavaEE版的eclipse开发动态的WEB工程(JavaWEB 项目)1).把开发选项切换到JavaEE2).可以在window -> Show View 中找到Package Ex ...

  3. 《周易》中的君子形象--http://cul.china.com.cn/guoxue/2018-06/04/content_40369049.htm

    中国文学本质上是一种君子文学,君子是中国文学的创作主体,君子与小人的人格冲突是中国文学矛盾冲突的主要形式.最早的君子是居住于城邦的贵族,而西周以来这一语词的道德化倾向愈来越重,渐渐摆脱了阶级意义而成为 ...

  4. Go 验证是否字符串包含中文

    发现一个验证字符串是否包含中文滴时候,一个比正则更好使滴方法,而且是golang 自带滴验证. 不需要自己写正则验证,代码如下: package main import ( "fmt&quo ...

  5. tensorflow函数解析:Session.run和Tensor.eval

    原问题链接: http://stackoverflow.com/questions/33610685/in-tensorflow-what-is-the-difference-between-sess ...

  6. SALESGROSSSALES_成本_利润

    //获取成本GETCOST_TMP:NoConcatenateLOAD T_SAL_OUTSTOCK.LE_ID, [T_SAL_OUTSTOCK.LCY CODE], T_SAL_OUTSTOCK. ...

  7. 虚拟机virtualBox

    在笔记本了装了一个虚拟机, 并安装了Linux系统, 方便测试linux 命令. 考虑到不需要图形界面, 学习了用命令行操作虚拟机, 配置如下 linux 下安装openssh-server 虚拟机设 ...

  8. ruoyi ShiroUtils

    package com.ruoyi.framework.util; import org.apache.shiro.SecurityUtils; import org.apache.shiro.cry ...

  9. Android Studio 停靠模式(Docked Mode)

    如果之前选了任务一种模式,先全都取消了 然后点击Window -->Active Tool Window-->这个时候就可以选择Docked Mode了

  10. 1. 模块化的引入与导出 (commonJS规范 和ES6规范)

    node组件导出模块 node一般用commonJS规范 可以通过module.exports导出自己写的模块 这样其他的js文件就可以引用并使用这个模块 module.exports = { log ...