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

数组-螺旋矩阵

M螺旋矩阵 ||(leetcode59)

image.png

/*** @param {number} n* @return {number[][]}*/
var generateMatrix = function(n) {const maxNum = n * n;let curNum = 1;const matrix = new Array(n).fill(0).map(() => new Array(n).fill(0));let row = 0,column = 0;const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]; //右下左上let directionIndex = 0;while (curNum <= maxNum) {matrix[row][column] = curNum;curNum++;const nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];if (nextRow < 0 || nextRow >= n || nextColumn < 0 || nextColumn >= n || matrix[nextRow][nextColumn] !== 0) {directionIndex = (directionIndex + 1) % 4; //顺时针旋转至下一个方向}row = row + directions[directionIndex][0];column = column + directions[directionIndex][1];}return matrix;
};

M螺旋矩阵(leetcode54)

image.png

/*** @param {number[][]} matrix* @return {number[]}*/
var spiralOrder = function(matrix) {let left = 0; //四个边界let right = matrix[0].length - 1;let top = 0;let bottom = matrix.length - 1;let res = []; //存储结果while(true) {for(let i = left; i <= right; i++) {res.push(matrix[top][i]);}top++;if (top > bottom) break; // 四个边,每个边结束都判断是否终止for (let i = top; i <= bottom; i++) {res.push(matrix[i][right]);}right--;if (right < left) break;for (let i = right; i >= left; i--) {res.push(matrix[bottom][i]);}bottom--;if(bottom < top) break;for(let i = bottom; i >= top;i--) {res.push(matrix[i][left]);}left++;if(left > right) break;}return res;
};

E螺旋遍历二维数组(leetcode146)

image.png

/*** @param {number[][]} array* @return {number[]}*/
var spiralArray = function(array) {if (!array.length || !array[0].length) {return [];}const rows = array.length, columns = array[0].length;const visited = new Array(rows).fill(0).map(() => new Array(columns).fill(false));const total = rows * columns;const order = new Array(total).fill(0);let directionIndex = 0, row = 0, column = 0;const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];for (let i = 0; i < total; i++) {order[i] = array[row][column];visited[row][column] = true;const nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];if (!(0 <= nextRow && nextRow < rows && 0 <= nextColumn && nextColumn < columns && !(visited[nextRow][nextColumn]))) {directionIndex = (directionIndex + 1) % 4;}row += directions[directionIndex][0];column += directions[directionIndex][1];}return order;
};
http://www.lryc.cn/news/390640.html

相关文章:

  • GitStack详细配置与使用指南
  • LoadRunner-Virtual User Generator组件学习
  • NAT地址转换实验,实验超简单
  • pip常用命令详解
  • vue3从入门到精通
  • kubuadm 方式部署 k8s 集群
  • Android studio 打包低版本的Android项目报错
  • 【教程】lighttpd配置端口反向代理
  • 微服务之服务保护策略【持续更新】
  • 微信小程序的开发
  • Oracle中CREATE FORCE VIEW的说明和例子
  • C#反射基本应用
  • 1.英语中的从句学习
  • Perl语言简介
  • 【SpringBoot3】使用Jasypt加密数据库用户名、密码等敏感信息
  • 如何确定MySQL中哪些列适合做索引
  • C# winform中权限页面的设计和开发
  • 本地Windows电脑 连接 Windows 服务器
  • 【分布式计算框架 MapReduce】MapReduce 初级编程
  • VideoPrism——探索视频分析领域模型的算法与应用
  • Spring Boot项目的两种发布方式
  • Java中的服务注册与发现原理与实现
  • 【Python】成功解决TypeError: ‘float‘ object cannot be interpreted as an integer
  • Java面试八股文
  • 周周星分享7.3—基于气象大数据的自动站实况联合预测
  • 【密码学】面向小白的古典密码基础入门笔记
  • 【Qt】之【Bug】大量出现“未定义的标识符”问题
  • C++中的常成员函数
  • 小试牛刀-区块链代币锁仓(Web页面)
  • Geoserver源码解读五 Catalog