当前位置: 首页 > news >正文

leetcode 885. Spiral Matrix III

题目链接

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.lryc.cn/news/426001.html

相关文章:

  • mysql windows安装与远程连接配置
  • 子网掩码是什么以及子网掩码相关计算
  • 仿RabbitMQ实现消息队列
  • SpringBoot教程(二十三) | SpringBoot实现分布式定时任务之xxl-job
  • 微前端架构的数据持久化策略与实践
  • 讲解 狼人杀中的买单双是什么意思
  • 回归分析系列5-贝叶斯回归
  • oracle 数据中lsnrctl 是干啥的
  • Linux进程--进程地址空间
  • C语言传递指针给函数
  • 探索 Kubernetes 持久化存储之 Rook Ceph 初窥门径
  • 今日(2024 年 8 月 13 日)科技新闻
  • Unity大场景切换进行异步加载时,如何设计加载进度条,并配置滑动条按照的曲线给定的速率滑动
  • Selenium + Python 自动化测试16(Python基础复习)
  • 2024新型数字政府综合解决方案(六)
  • vscode的C/C++环境配置和调试技巧
  • Python制作《扫雷》游戏-附源码,轻松看懂,简单易学
  • 8路VBO转HDMI2.0支持4K60频率ITE6265芯片方案心得分享
  • 【问题解决3】【已解决】Cannot determine path to‘tools.jar‘libraryfor17
  • 基于Spring Boot的高效宠物购物平台
  • 【Vue3】路由基础
  • 掌握网络数据的钥匙:Python Requests-HTML库深度解析
  • 网络安全: 模型的脆弱性,鲁棒性和隐私性
  • 【go语言】go-webview2用法(持续更新)
  • KNN 图像识别
  • 基于STM32和云平台的花卉养护系统设计(微信小程序)(209)
  • 编程语言进化史
  • vuex的原理和使用方法
  • (javaweb)SpringBootWeb案例(毕业设计)案例--文件上传
  • 数据库之存储过程和函数