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 ------------------------------------------------------------------- ...
随机推荐
- [转]DELL PERC 系列阵列卡选型和用法指南
引用地址 https://www.sulabs.net/?p=895 DELL PERC 系列阵列卡选型和用法指南 2018年12月29日 Su 本文缘起于一位朋友在生产服务器硬件中,使用了错误的阵列 ...
- web录音——上传录音文件
捕获麦克风 一. 前言 公司项目需要实现web录音,刚刚好接手此功能,由于之前未接触过,在网上找了些资料做对比 ) https://www.cnblogs.com/starcrm/p/51 ...
- js对象的所有方法
Object构造方法 Object.assign() 将所有可枚举的自身属性的值从一个或多个源对象复制到目标对象. Object.create() 用指定的原型对象和属性创建一个新对象. Object ...
- SQL 语句 连接
SQL连接可以分为内连接.外连接.交叉连接. 数据库数据: book表 stu表 1.内连接 ...
- 手摸手教你使用vue-cli脚手架-详细步骤图文解析[vue入门]
写在前面: 使用 vue-cli 可以快速创建 vue 项目,vue-cli很好用,但是在最初搭建环境安装vue-cli及相关内容的时候,对一些人来说是很头疼的一件事情,本人在搭建vue-cli的项目 ...
- Pandas中DataFrame数据合并、连接(concat、merge、join)之merge
二.merge:通过键拼接列 类似于关系型数据库的连接方式,可以根据一个或多个键将不同的DatFrame连接起来. 该函数的典型应用场景是,针对同一个主键存在两张不同字段的表,根据主键整合到一张表里面 ...
- Java-Base64Fiend工具类
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; impo ...
- Machine Learning(Andrew Ng)学习笔记
1.监督学习(supervised learning)&非监督学习(unsupervised learning) 监督学习:处理具有若干属性且返回值不同的对象.分为回归型和分类型:回归型的返回 ...
- vue项目中,点击按钮复制其内容
<el-table-column label="推广链接" align="center"> <template slot-scope=&quo ...
- 给PS添加ICO格式文件
为什么你的ps不能直接打开favicon.ico文件呢?因为你没有安装识别ico的格式插件. 安装步骤如下: 下载格式文件:https://pan.baidu.com/s/1lE0El1VtDqD5l ...