leetcode 885. Spiral Matrix III

embedded/2024/10/16 2:26:47/

题目链接

You start at the cell (rStart, cStart) of an rows x cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.

You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all rows * cols spaces of the grid.

Return an array of coordinates representing the positions of the grid in the order you visited them.

题解:

Medium的题无需多言,直接上代码:

class Solution {
public:vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {int drc[4][2] = {{0,1},{1,0},{0,-1},{-1,0}},dn = 0, dp[2] = {1,1}, end = rows*cols, ln=0;vector<vector<int>> rst(end, {0,0});rst[ln][0] = rStart;rst[ln++][1] = cStart;while(ln < end) {for(int i = 0; i< dp[dn&1]; i++) {rStart += drc[dn][0];cStart += drc[dn][1];if (isInMatrix(rStart, rows) && isInMatrix(cStart, cols)) {rst[ln][0] = rStart;rst[ln++][1] = cStart;}}++dp[dn&1];(++dn) %= 4;}return rst;}inline bool isInMatrix(int x, int ln) {return x>= 0 && x <ln;}
};


http://www.ppmy.cn/embedded/96934.html

相关文章

优先级队列的实现

什么是优先级队列 优先级队列是一种特殊的数据结构&#xff0c;它类似于队列或栈&#xff0c;但是每个元素都关联有一个优先级或权重。在优先级队列中&#xff0c;元素的出队顺序不是简单地按照它们进入队列的先后顺序&#xff08;先进先出&#xff0c;FIFO&#xff09;&#…

【安卓】多线程编程

文章目录 线程的简单应用解析异步消息处理机制使用AsyncTask 线程的简单应用 新建一个AndroidThreadTest项目&#xff0c;然后修改activity_main.xml中的代码。 <RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width…

Pytorch:加载断点(pth)权重参数

一、保存的模型参数及权重 #保存模型 torch.save(model_object,resnet.pth) #加载模型 modeltorch.load(resnet.pth)二、仅保存模型的权重 torch.save(my_resnet.state_dict(),"resnet.pth")resnet_model.load_state_dict(torch.load("resnet.pth"))三、仅…

ilo地址是什么

ilo地址是什么&#xff1f; iLO 地址一般是服务器的专用网络接口的 IP 地址&#xff0c;用于在服务器本地控制台不可用时对服务器进行远程管理和监控&#xff0c;例如进行远程开机、关机、安装操作系统、查看硬件状态等操作。 要获取 iLO 地址&#xff0c;通常可以在服务器的…

Redis的缓存淘汰策略

1. 查看Redis 最大的占用内存 打开redis配置文件, 设置maxmemory参数&#xff0c;maxmemory 是bytes字节类型, 注意转换 2. Redis默认内存多少可以用 注意: 在64bit系统下&#xff0c; maxmemory 设置为 0 表示不限制Redis内存使用 3. 一般生产上如何配置 一般推荐Redis 设置内…

IP基础(通俗易懂版)

IP 位于 TCP/IP 参考模型的第三层&#xff0c;也就是⽹络层。 ⽹络层的主要作⽤是&#xff1a;实现主机与主机之间的通信&#xff0c;也叫点对点通信。 1 、网络层&#xff08; IP) 与数据链路层 (MAC) 有什么关系呢&#xff1f; MAC 的作用&#xff1a; 实现【直连】的两个…

设计模式 - 状态模式

目录 1. 前言 2. 基本原理 3. UML模型 4. 例程 1. 前言 状态模式作为设计模式的一种&#xff0c;主要用于根据状态的改变执行不同的动作,它允许一个对象在其内部状态改变时改变它的行为。状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况。把状态的…

一个手机到手机之间通话经过了哪些设备

来源&#xff1a;https://www.bilibili.com/video/BV1ic411F7mM/?spm_id_from333.880.my_history.page.click&vd_source6c5d3cd50fc7fa8732bdfb760a055839 一个手机通话需要经过下面三个网络 类别接入网&#xff08;Access Network&#xff09;承载网&#xff08;Transp…