885-螺旋矩阵 - III
885-螺旋矩阵 - III
在 R 行 C 列的矩阵上,我们从 (r0, c0) 面朝东面开始
这里,网格的西北角位于第一行第一列,网格的东南角位于最后一行最后一列。
现在,我们以顺时针按螺旋状行走,访问此网格中的每个位置。
每当我们移动到网格的边界之外时,我们会继续在网格之外行走(但稍后可能会返回到网格边界)。
最终,我们到过网格的所有 R * C 个空间。
按照访问顺序返回表示网格位置的坐标列表。
示例 1:
输入:R = 1, C = 4, r0 = 0, c0 = 0
输出:[[0,0],[0,1],[0,2],[0,3]]
示例 2:
输入:R = 5, C = 6, r0 = 1, c0 = 4
输出:[[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
提示:
- 1 <= R <= 100
- 1 <= C <= 100
- 0 <= r0 < R
- 0 <= c0 < C
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/spiral-matrix-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {
int[][] res = new int[R * C][2];
int num = 0;
int cnt = 1;
boolean[][] visited = new boolean[R][C];
while (num < R * C) {
for(int j = c0 - cnt + 1; j <= c0 + cnt; j++) {
if(r0 + 1 - cnt >= 0 && r0 + 1 - cnt < R && j >= 0 && j < C
&& !visited[r0 + 1 - cnt][j]) {
res[num++] = new int[]{r0 - cnt + 1, j};
visited[r0 + 1 - cnt][j] = true;
}
}
for(int i = r0 - cnt + 1; i <= r0 + cnt; i++) {
if(i >= 0 && i < R && c0 + cnt >= 0 && c0 + cnt < C
&& !visited[i][c0 + cnt]) {
res[num++] = new int[]{i, c0 + cnt};
visited[i][c0 + cnt] = true;
}
}
for (int j = c0 + cnt; j >= c0 - cnt; j--) {
if(r0 + cnt >= 0 && r0 + cnt < R && j >= 0 && j < C
&& !visited[r0 + cnt][j]) {
res[num++] = new int[]{r0 + cnt, j};
visited[r0 + cnt][j] = true;
}
}
for (int i = r0 + cnt; i >= r0 - cnt; i--) {
if(i >= 0 && i < R && c0 - cnt >= 0 && c0 - cnt < C
&& !visited[i][c0 - cnt]) {
res[num++] = new int[]{i, c0 - cnt};
visited[i][c0 - cnt] = true;
}
}
cnt++;
}
return res;
}
官方题解:
class Solution {
public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {
int[] dr = new int[]{0, 1, 0, -1};
int[] dc = new int[]{1, 0, -1, 0};
int[][] ans = new int[R*C][2];
int t = 0;
ans[t++] = new int[]{r0, c0};
if (R * C == 1) return ans;
for (int k = 1; k < 2*(R+C); k += 2)
for (int i = 0; i < 4; ++i) { // i: direction index
int dk = k + (i / 2); // number of steps in this direction
for (int j = 0; j < dk; ++j) { // for each step in this direction...
// step in the i-th direction
r0 += dr[i];
c0 += dc[i];
if (0 <= r0 && r0 < R && 0 <= c0 && c0 < C) {
ans[t++] = new int[]{r0, c0};
if (t == R * C) return ans;
}
}
}
throw null;
}
}
作者:LeetCode
链接:https://leetcode-cn.com/problems/spiral-matrix-iii/solution/luo-xuan-ju-zhen-iii-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
885-螺旋矩阵 - III的更多相关文章
- [Swift]LeetCode885. 螺旋矩阵 III | Spiral Matrix III
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- PAT 1050. 螺旋矩阵(25)
本题要求将给定的N个正整数按非递增的顺序,填入"螺旋矩阵".所谓"螺旋矩阵",是指从左上角第1个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为m行n列,满足条 ...
- leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?
Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- Java-基础编程(螺旋矩阵&乘法表)
package cn.rick.study; import java.io.BufferedReader;import java.io.InputStreamReader;import java.ut ...
- NOIP 2014 普及组 T3 螺旋矩阵
[题意] 已知:n,r,c(n<=30000) 条件:给定n行n列的螺旋矩阵(从矩阵的左上角(1,1)出发,初始时向右移动:如果前方是未曾经过的格子, 则继续前进,否则右转:重复上述操作直至经过 ...
- PAT-乙级-1050. 螺旋矩阵(25)
1050. 螺旋矩阵(25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求将给定的N个正整数按非递增的 ...
- NOIP2014-普及组复赛-第三题-螺旋矩阵
题目描述 Description 一个n行n列的螺旋矩阵可由如下方法生成: 从矩阵的左上角(第1行第1列)出发,初始时向右移动:如果前方是未曾经过的格子,则继续前进,否则右转:重复上述操作直至经过矩阵 ...
- LeetCode 59. Spiral Matrix II (螺旋矩阵之二)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- LeetCode 54. Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
随机推荐
- Day6前端学习之路——布局
一.定位 1)静态定位 position:static(默认) 2)相对定位 position:relative(要配合top.bottom.left.right等属性来使用) 3)绝对定位 pos ...
- css实现文字过长显示省略号的方法
<div class="title">当对象内文本溢出时显示省略标记</div> 这是一个例子,其实我们只需要显示如下长度: css实现网页中文字过长截取. ...
- 第二篇 Springboot mybatis generate根据数据库表自动生成实体类、Mapper和Mapper.xml
源码链接:https://pan.baidu.com/s/1iP4UguBufHbcIEv4Ux4wDw 提取码:j6z9 目录结构如下:只需增加一个generatorConfig.xml文件和在po ...
- 图解Java设计模式之设计模式面试题
图解Java设计模式之设计模式面试题 1.1 Java设计模式内容介绍 1.1.1 先看几个经典的面试题 1.1.2 设计模式的重要性 1.1 Java设计模式内容介绍 1.1.1 先看几个经典的面试 ...
- [Contract] Solidity 遍历 mapping 的一种方式
思路:为需要遍历的 mapping 再准备一个 list,之后通过 for 循环遍历 list 取得 mapping 的 key. mapping (address => uint) users ...
- IIS6的文件解析漏洞
IIS6的默认配置漏洞会把cer.cdx.asa作为asp代码来解析 后缀解析漏洞 /test.asp;.jpg 或者/test.asp:.jpg(此处需抓包修改文件名) IIS6.0 都会把此类后缀 ...
- HDU-1754 I Hate It (树状数组模板题——单点更新,区间查询最大值)
题目链接 ac代码(注意字符读入前需要注意回车的影响) #include<iostream> #include<cstdio> #include<cstring> ...
- #6041. 「雅礼集训 2017 Day7」事情的相似度 [set启发式合并+树状数组扫描线]
SAM 两个前缀的最长后缀等价于两个点的 \(len_{lca}\) , 题目转化为求 \(l \leq x , y \leq r\) , \(max\{len_{lca(x,y)}\}\) // p ...
- python 访问sql server数据库
访问数据库 cnxn = pyodbc.connect("Driver={SQL Server};Server=localhost;Database=用户名;uid=sa;pwd=密码&qu ...
- 在Windows7中的各种显示模式中桌面图标的尺寸
在Windows7中的各种显示模式中,图标的尺寸 window7 桌面icon设计尺寸大小桌面图标设计尺寸一般是多少超大图标:256X256大图标:128X128中图标:32X32平铺:32X32列表 ...