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. JavaMaven【四、坐标&构件】

    maven的依赖都是使用坐标找到对应的构件来进行的 坐标 即groupId+artifactId+version 上图第一个红框是本项目的坐标 第二个红框是依赖的项目的坐标 构件 坐标对应的jar包 ...

  2. python中F/f表达式优于format()表达式

    F/f表达式可以解析任意类型的数据 具体实现,看下面示例: 1.解析变量 1 a = 10 3 b = 20 5 res1 = F"a+b的值:{a+b}" 7 print(res ...

  3. fastadmin 页面添加编辑日期时间

    添加 <div class="form-group"> <label class="control-label col-xs-12 col-sm-2&q ...

  4. 详解python中的生成器表达式

    什么是生成器表达式 还记得列表解析吗?我们把[]换成()就变成生成器表达式了. g = (x for x in [1, 2, 3, 4]) print(g) # <generator objec ...

  5. kettle Spoon.bat闪退解决办法

    1.Java环境配置问题 java_home:D:\Program Files\Java\jdk1.7.0_25(安装jdk路径) classpath:.;%java_home%\lib\dt.jar ...

  6. 第1章 python入门

    1.1 python的出生与应用   python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时间,决心开 ...

  7. 平衡搜索树-B树。

    B Tree 系列 摘录: https://blog.csdn.net/v_JULY_v/article/details/6530142 B+树介绍 B+树的几点介绍 动态查找树有: 二叉查找树,自平 ...

  8. Django:报错 raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)

    Django 执行迁移生成表: python manage.py migrate 报错: raise MigrationSchemaMissing("Unable to create the ...

  9. Python学习笔记:序列构成的数组

    列表推导是一种构建列表(list)的快捷方式 #列表推导 symbols = '!@#$%' codes = [ord(symbol) for symbol in symbols] #ord()Pyt ...

  10. 利用jQuery实现图片无限循环轮播(不借助于轮播插件)

    原来我主要是用Bootstrap框架或者swiper插件实现轮播图的功能,而这次是用jQuery来实现图片无限循环轮播! 用到的技术有:html.css.JavaScript(少).jQuery(主要 ...