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 example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
【题目分析】
与Spiral Matrix不同,这个题目要求生成一个方阵,方阵的值是螺旋递增的。
【思路】
其实这个题目与上一个大同小异,他们遍历矩阵的顺序是相同的,只要把取值变为赋值就可以了。
【java代码】
public class Solution {
public int[][] generateMatrix(int n) {
int[][] matrix = new int[n][n];
int top = 0;
int bottom = n-1;
int left = 0;
int right = n-1;
int num = 1;
while(true){
for(int i = left; i <= right; i++) matrix[top][i] = num++;
top++;
if(left > right || top > bottom) break;
for(int i = top; i <= bottom; i++) matrix[i][right] = num++;
right--;
if(left > right || top > bottom) break;
for(int i = right; i >= left; i--) matrix[bottom][i] = num++;
bottom--;
if(left > right || top > bottom) break;
for(int i = bottom; i >= top; i--) matrix[i][left] = num++;
left++;
if(left > right || top > bottom) break;
}
return matrix;
}
}
LeetCode OJ 59. Spiral Matrix II的更多相关文章
- 【一天一道LeetCode】#59. Spiral Matrix II
一天一道LeetCode系列 (一)题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- 【LeetCode】59. Spiral Matrix II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
- 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 ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- [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 ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
- 59. Spiral Matrix II
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
随机推荐
- css-ie6下实现最小,最大宽度
_width: expression((document.documentElement.clientWidth||document.body.clientWidth)<1040?"1 ...
- js的特殊运算符
1)三元条件运算符: c是一个布尔值,当c为true的时候,取冒号左边a的值,否取冒号右边的b的值: 2)逗号运算符: 值从左到右依次计算,取最右边的,例如例子里的val,会取最右边的值3: 特殊运算 ...
- mac 搭建node 开发环境记录
安装homebrew: enter 键 后 输入电脑密码 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/i ...
- Java I/O演进与Linux网络I/O模型
参考文章: 简书-浅谈Linux五种IO:http://www.jianshu.com/p/486b0965c296 一.linux基础概念 1.1 内存空间 linux系统中的使用的是虚拟存储器,即 ...
- Chronodex:视觉时间管理,让你的生活更有序
我喜欢把时间安排的有条不紊,看看清晰的时间安排心理有种踏实感,只有你是"纸爱好者" - 才能最终寻找完美组织时间的方式方法. 我记得自从我是一个小女孩以来,我喜欢纸和笔和颜色和标记 ...
- js输入框对金额的匹配
/** * 金额格式化 * @param s * @param n * @returns {String} */ function fmoney(s, n) { n = n > 0 && ...
- photoshop切图
1.首先需要用photoshop把psd源文件打开,看看源文件的整体布局.源文件是分层的,方便切图的层次. 2.切图的工具叫做"切片",在左侧面板可以看到.右击可以看到" ...
- Linux kernel Vhost-net 和 Virtio-net代码详解
场景 Host上运行qemu kvm虚拟机,其中虚拟机的网卡类型为virtio-net,而Host上virtio-net backend使用vhost-net 数据包进入虚拟机代码分析 首先看vhos ...
- php 编程效率(3)
提高php编程效率的53个小知识点:用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中 搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当 ...
- 模仿快递路线图的html, css 样式
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...