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的更多相关文章

  1. [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 ...

  2. LeetCode 885. Spiral Matrix III

    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...

  3. 【LeetCode】885. Spiral Matrix III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. [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 ...

  5. [Solution] 885. Spiral Matrix Ⅲ

    Difficulty: Medium Problem On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) f ...

  6. 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 ...

  7. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  8. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

随机推荐

  1. Go学习笔记【一、概述】

    工作需要,即将要把部分查询计算的逻辑翻译成Go语言,来提升查询效率 在此需要对Go语言进行学习,先把入门教程浏览了一遍,发现有些语法和C很类似,还有部分类似于Python,还没有深入学习,先开个坑,业 ...

  2. selectpage

    官方文档地址 https://terryz.oschina.io/selectpage/docs.html

  3. linux 网卡相关命令

    1. ifconfig //查看网络相关信息 2. ifconfig eth0 192.168.1.103 netmask 255.255.255.0 //配置eth0的IP地址 3. route - ...

  4. windows消息的循环机制

    首先来了解几个基本概念: 消息:在了解什么是消息先来了解什么是事件.事件可分为几种,由输入设备触发的,比如鼠标键盘等等.由窗体控件触发的,比如button控件,file菜单等.还有就是来自Window ...

  5. 51Nod 1534 棋盘阻挡博弈

    很简单的可以知道 如果P在V的右上角 必输 如果P在V的左下角 必赢 接下里还剩下左上角和右下角两种情况 两种情况其实相同 P是挡不住V通过对角线方向向下/左的移动的 即两者不会相互影响 所以我们只要 ...

  6. springboot2.1.7整合Druid

    一.maven的依赖:文中就贴重点的, 其他依赖就不贴了 <dependency> <groupId>com.alibaba</groupId> <artif ...

  7. HTTPClick调用WebApi帮助类

    1.创建ApiHelper类 2.复制以下代码到类中 using System; using System.Collections.Generic; using System.Linq; using ...

  8. Httpd总结 :HTTPD的基本概念

    这是一篇为初学者准备的文章,所以作者会尽量从基础出发,尽量细致的描述每一个细节,以求让初学者不会一头雾水,有一定基础的同学就不用看了,以免浪费你的时间.   假设博主今天春心荡漾,想要访问一些不可描述 ...

  9. Linux常用命令type、date

    Linux命令类型: 内置命令(shell内置):cd is shell builtin 外部命令:命令 is /usr/bin/命令,在文件系统的某个路径下有一个与命令名称相应的可执行文件 type ...

  10. java 面试题摘要

    5.1 什么是线程 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程的实际运作单位, 可以使用多线程对运算提速. 5.2 线程和进程有什么区别 (1)进程是资源的分配和调度的一个独 ...