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 ------------------------------------------------------------------- ...
随机推荐
- JAVA语言程序设计课后习题----第三单元解析(仅供参考)
1 本题水题,记住要知道输入格式即可 import java.util.Scanner; public class test { public static void main(String[] ar ...
- c++11 移动语义move semantics
performance, expensive object copies move semantics, temporary objects implemented with rvalue refer ...
- spring-02
spring-02 1.谈谈你对 Spring 的理解 Spring 是一个开源框架,为简化企业级应用开发而生.Spring 可以是使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.S ...
- PAT 1003.我要通过1
答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: ...
- POI读取Excel如何判断行为空
public static boolean isRowEmpty(Row row) { for (int c = row.getFirstCellNum(); c < row.getLastCe ...
- 计划任务 at,cron
示例:每3小时echo和wall命令
- 题解 【NOIP2016】组合数问题
[NOIP2016]组合数问题 Description Input 第一行有两个整数t, k,其中t代表该测试点总共有多少组测试数据,k的意义见[问题描述]. 接下来t行每行两个整数n, m,其中n, ...
- Python之concurrent.futures模块的使用
concurrent.futures的作用: 管理并发任务池.concurrent.futures模块提供了使用工作线程或进程池运行任务的接口.线程和进程池API都是一样,所以应用只做最小 ...
- 基于node.js的websocket 前后端交互小功能
一.node var ws = require("nodejs-websocket"); console.log("开始建立连接...") var server ...
- HTML的多媒体属性
㈠什么是多媒体? 多媒体来自多种不同的格式.它可以是听到或看到的任何内容,文字.图片.音乐.音效.录音.电影.动画等等. Web 上的多媒体指的是音效.音乐.视频和动画. 确定媒体类型的最常用的方法是 ...