【算法题】矩阵顺时针旋转90° (js)

力扣链接:https://leetcode.cn/problems/rotate-matrix-lcci/description/

本人题解:

js 复制代码
/**
 * @param {number[][]} matrix
 * @return {void} Do not return anything, modify matrix in-place instead.
 */
var rotate = function (matrix) {
    const x = matrix.length || 0;
    const y = x > 0 ? matrix[0]?.length : 0;
    if (x === 0) return [];
    if (y === 1) return matrix;
    for (let i = 0; i < x; i++) {
        for (let j = i; j < y; j++) {
            const temp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = temp;
        }
    }
    return matrix.map((item) => item.reverse());
};
相关推荐
陈振wx:zchen20081 小时前
JavaScript
javascript·js
有时间要学习1 小时前
面试150——第五周
算法·深度优先
我是伪码农1 小时前
Vue 智慧商城项目
前端·javascript·vue.js
不认输的西瓜1 小时前
fetch-event-source源码解读
前端·javascript
晚霞的不甘2 小时前
Flutter for OpenHarmony 可视化教学:A* 寻路算法的交互式演示
人工智能·算法·flutter·架构·开源·音视频
望舒5132 小时前
代码随想录day25,回溯算法part4
java·数据结构·算法·leetcode
C++ 老炮儿的技术栈2 小时前
Qt 编写 TcpClient 程序 详细步骤
c语言·开发语言·数据库·c++·qt·算法
KYGALYX2 小时前
逻辑回归详解
算法·机器学习·逻辑回归
铉铉这波能秀2 小时前
LeetCode Hot100数据结构背景知识之集合(Set)Python2026新版
数据结构·python·算法·leetcode·哈希算法
踢足球09292 小时前
寒假打卡:2026-2-8
数据结构·算法