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. HTML超链接实例介绍

    <html><head><title>第六节课</title><meta charset="UTF-8"></he ...

  2. PAT Advanced 1115 Counting Nodes in a BST (30) [⼆叉树的遍历,BFS,DFS]

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  3. XCOM串口助手打印不出数据

    本次实验是在基于原子的战舰开发板上的做定时器捕获实验,程序源码下载到板子上运行正常.指示灯正常显示,打开XCOM识别不来串口,原因:硬件上没有插USB转串口线: 连接上USB转串口线,软件上以显示CH ...

  4. 客户主题分析(tableau)—客户留存

    客户留存分析(客户漏斗分析),关键在于找到影响客户留存的因素,设计场景测试,验证关键因素.即可以通过关键因素影响留存,从而重塑客户漏斗到更有价值的形状. 案例:母婴产品客户留存分析 数据结构:  1) ...

  5. mysql的show status和show global status区别在哪

    show status                   本次会话的参数状态show global status        本次MYSQL服务开启(或重置)到现在总请求数

  6. OpenMP笔记(一)

    原文:https://www.bearoom.xyz/2019/02/17/openmp1/ 并行技术有很多种,OpenMP算是比较简单可用的一种,OpenMP全称是 Open Multi-Proce ...

  7. 转:以下是目前已经建立的sub一览 来自:https://zhuanlan.zhihu.com/p/91935757

    转:以下是目前已经建立的sub一览  来自:https://zhuanlan.zhihu.com/p/91935757 作者: Lorgar 理工科 科学(和英文r/science一样,只接受论文讨论 ...

  8. E - Two Arithmetic Progressions(CodeForces - 710D)(拓展中国剩余定理)

    You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such ...

  9. Codeforces Round #555 (Div. 3) B. Long Number 【仔细读题】

    B. Long Number time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  10. flask汇总

    flask框架 蓝图 随着flask程序越来越复杂,我们需要对程序进行模块化的处理,之前学习过python的模块化管理,于是针对一个简单的flask程序进行模块化处理 Blueprint概念 简单来说 ...