Leetcode 54.螺旋矩阵 JavaScript (Day 8)

javascript 复制代码
var spiralOrder = function(matrix) {
    let ans=[];
    let left =0,right=matrix[0].length-1;
    let top=0,bottom=matrix.length-1;
    while(left<right&&top<bottom){
        for(let i=left;i<right;i++) ans.push(matrix[top][i]);
        for(let i=top;i<bottom;i++) ans.push(matrix[i][right]);
        for(let i=right;i>left;i--) ans.push(matrix[bottom][i]);
        for(let i=bottom;i>top;i--) ans.push(matrix[i][left]);
        left++,right--;
        top++,bottom--;
    }
    if(top==bottom){
        for(let i=left;i<=right;i++) ans.push(matrix[top][i]);
    }
    else if(left==right){
        for(let i=top;i<=bottom;i++) ans.push(matrix[i][left]);
    }    
    
    return ans;
};

算法核心:直接见图

相关推荐
mmmmath_34 小时前
LeetCode.541.反转字符串II
数据结构·算法·leetcode
Navigator_Z5 小时前
LeetCode //MySQL - 1251. Average Selling Price
c语言·算法·leetcode
参.商.6 小时前
【Day 53】76. 最小覆盖子串
leetcode·golang
虚无的纽扣9 小时前
【力扣刷题】第二天:无重复字符的最长字串、移动零问题
算法·leetcode·排序算法
默_笙9 小时前
🚋 从流水线到地铁网:为什么复杂 AI 都要拆成多 Agent(上)——LangGraph 基础入门
前端·javascript
ID346107442010 小时前
【课程设计】基于Spring Boot+Vue的游戏账号租赁系统的设计与实现-计算机毕设 附源码50345
javascript·vue.js·spring boot·python·node.js·php·课程设计
柚yuzumi10 小时前
彻底搞懂 JavaScript 类型转换:显式转换、隐式转换与 ToPrimitive
前端·javascript·node.js
AI情绪识别开源11 小时前
检信ALLEMOTION 认知评估分析器 · WebSocket 推送数据文档
javascript
OpsEye11 小时前
公司内部全员使用 AI,如何保证会话合规、行为可审计?
javascript·ai编程