885. Spiral Matrix III
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east.
Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the grid is at the last row and column.
Now, we walk in a clockwise spiral shape to visit every position in this grid.
Whenever we would move outside the boundary of the grid, we continue our walk outside the grid (but may return to the grid boundary later.)
Eventually, we reach all R * C spaces of the grid.
Return a list of coordinates representing the positions of the grid in the order they were visited.
Example 1:
Input: R = 1, C = 4, r0 = 0, c0 = 0
Output: [[0,0],[0,1],[0,2],[0,3]]

Example 2:
Input: R = 5, C = 6, r0 = 1, c0 = 4
Output: [[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]]

Runtime: 72 ms, faster than 32.30% of C++ online submissions for Spiral Matrix III.
大概思路就是不要考虑限制和边界,把路径都走一遍,然后符合题意的放进结果。
#include <stack>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <vector>
#include <unordered_set>
#include "header.h"
using namespace std; class Solution {
public: int R;
int C;
bool canadd(int x, int y){
return x < R && x >= && y < C && y >= ;
} vector<vector<int>> spiralMatrixIII(int _R, int _C, int r0, int c0) {
R = _R;
C = _C;
int cur = , MaxSize = R * C, len = ,cnt = ;
vector<vector<int>> mtx = {{r0,c0}};
while(cur < MaxSize){
cnt = ; len++;
while(cnt < len && c0 < *C){
cnt++; c0++;
if(canadd(r0, c0)) {
mtx.push_back({r0, c0});
cur++;
}
}
cnt = ;
while(cnt < len && r0 < *R){
cnt++; r0++;
if(canadd(r0, c0)){
mtx.push_back({r0, c0});
cur++;
} }
cnt = ;
len++;
while(cnt < len && c0 >= -C+){
cnt++; c0--;
if(canadd(r0, c0)){
mtx.push_back({r0, c0});
cur++;
} }
cnt = ;
while(cnt < len && r0 >= -R+){
cnt++; r0--;
if(canadd(r0,c0)){
mtx.push_back({r0, c0});
cur++;
}
}
}
return mtx;
}
};
885. Spiral Matrix III的更多相关文章
- [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- LeetCode 885. Spiral Matrix III
原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...
- 【LeetCode】885. Spiral Matrix III 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- [Solution] 885. Spiral Matrix Ⅲ
Difficulty: Medium Problem On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) f ...
- leetcode 889. Spiral Matrix III
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- [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 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
随机推荐
- IIS出现Server Error in '/' Application 错误的解决办法
C:\Windows\Temp"文件夹加上 IIS_IUSRS的权限
- fastadmin 随笔 刷新表格数据 获取当前登录人信息 服务端导出Excel
table.bootstrapTable('refresh',{url:'你的url'}); 获取当前登录人信息 $this->auth就能获取当前用户信息,比如$this->auth-& ...
- CentOS7.x卸载与安装MySQL5.7的操作过程以及编码格式的修改
一.MySQL5.7的卸载 1.1yum方式查看yum是否安装过mysql cd yum list installed mysql* 如或显示了列表,说明系统中有MySQL 如上显示,我已经安装了my ...
- idea 下gradle创建springboot 项目
InterlijIdea 开发环境下创建基于springBoot的项目. 环境 1.jdk1.5以上 2.interlijidea 15 以上 步骤 1.File –>new –>Proj ...
- NOIP2017 Day1 T3 逛公园
NOIP2017 Day1 T3 更好的阅读体验 题目描述 策策同学特别喜欢逛公园.公园可以看成一张\(N\)个点\(M\)条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,\(N\)号点 ...
- shell命令学习记录
id id会显示用户以及所属群组的实际与有效ID hostname 用来显示或者设置主机名(show or set the system’s host name).环境变量HOSTNAME也保存了当前 ...
- python操作mysql代码讲解(及其实用,未来测试工作主要操作数据库的方法)
pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 下载安装 1 pip3 install pymysql 使用操作 1.执行SQL 1 2 3 4 ...
- Java io 理解
任何程序都有io部分,io是对程序来说数据流的输入和输出.这里说的流,是指有字节组成的列,不断输入程序,或者从程序中输出,我们形象称为流.Java的io流有两种,一种叫字节流,最原始的:一种叫字符流. ...
- python小练手题1
1. """ Write a program which can compute the factorial of a given numbers. The result ...
- Win7 右键 新建图标消失的解决办法
方法一: 把下面一段代码存在一个记事本上,再选择另存为1.cmd,最后运行! regsvr32 /u /s igfxpph.dll reg delete HKEY_CLASSES_ROOT\Direc ...