059 Spiral Matrix II 旋转打印矩阵 II
给出正整数 n,生成正方形矩阵,矩阵元素为 1 到 n2 ,元素按顺时针顺序螺旋排列。
例如,
给定正整数 n = 3,
应返回如下矩阵:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
详见:https://leetcode.com/problems/spiral-matrix-ii/description/
Java实现:
class Solution {
public int[][] generateMatrix(int n) {
int[][] res=new int[n][n];
if(n==0){
return res;
}
int top=0;
int bottom=n-1;
int left=0;
int right=n-1;
int num=1;
while(top<=bottom&&left<=right){
if(num<=n*n){
for(int j=left;j<=right;++j){
res[top][j]=num;
++num;
}
}
++top;
if(num<=n*n){
for(int i=top;i<=bottom;++i){
res[i][right]=num;
++num;
}
}
--right;
if(num<=n*n){
for(int j=right;j>=left;--j){
res[bottom][j]=num;
++num;
}
}
--bottom;
if(num<n*n){
for(int i=bottom;i>=top;--i){
res[i][left]=num;
++num;
}
}
++left;
}
return res;
}
}
059 Spiral Matrix II 旋转打印矩阵 II的更多相关文章
- 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】
[059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...
- 059. Spiral Matrix II
题目链接:https://leetcode.com/problems/spiral-matrix-ii/description/ Given a positive integer n, generat ...
- 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 ...
- 054 Spiral Matrix 旋转打印矩阵
给出一个 m x n 的矩阵(m 行, n 列),请按照顺时针螺旋顺序返回元素.例如,给出以下矩阵:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]应该返回 [1,2, ...
- 【LeetCode每天一题】Spiral Matrix II(螺旋数组II)
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...
- 【leetcode刷题笔记】Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [Leetcode] spiral matrix 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 螺旋矩阵 II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...
- C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...
随机推荐
- ACM学习历程—NPU1086 随机数 2015年陕西省程序设计竞赛网络预赛(正式赛)C题 (计数排序 || set容器)
Description 开学了,ACM队的边老板想在学校中请一些妹子一起做一项问卷调查,调查妹子们对ACM的了解情况,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100), ...
- 1070 Bash 游戏 V4
传送门 1070 Bash游戏 V4 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次拿的数量最少1个,最多 ...
- python爬虫知识点总结(十)分析Ajax请求并抓取今日头条街拍美图
一.流程框架
- JAVA + SELENIUM--环境搭建
一.安装JDK 可在JAVA官网http://www.java.com/zh_CN/download/ 中下载最新的JDK,并按提示安装 环境变量配置:我的电脑右键-->属性--&g ...
- 正则表达式 LINUX
正则表达式 热身 正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串.将匹配的子串做替换或者从某个串中取出符合某个条件的子串等. 例如 g ...
- 使用BIND安装智能DNS服务器(一)---基本的主从DNS服务器搭建
参考网址:http://www.unixmen.com/dns-server-installation-step-by-step-using-centos-6-3/ DNS(Domain Name S ...
- Windows下git的安装与配置
表示git安装成功.
- 实训随笔4:HTML初入门
1.<td>与<tr>标签 表格制作时,应该一行一行的画,即<tr>应该包含<td>标签,正确示例如下: <h3>测试数组初始化与操作< ...
- Android使用SO库时要注意的一些问题
转自:https://segmentfault.com/a/1190000005646078 正好动态加载系列文章谈到了加载SO库的地方,我觉得这里可以顺便谈谈使用SO库时需要注意的一些问题.或许这些 ...
- —Libre#2009. 「SCOI2015」小凸玩密室
#2009. 「SCOI2015」小凸玩密室 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...