leetcode-顺时针旋转矩阵-111

题目要求

思路

1.假设现在有一个矩阵

123

456

789

2.我们可以根据19这个对角线将数据进行交换,得到矩阵

147

258

369

3.然后将矩阵每一行的数据再翻转,得到矩阵

741

852

963

代码实现

cpp 复制代码
class Solution {
public:
    vector<vector<int> > rotateMatrix(vector<vector<int> >& mat, int n) {
        for(int i = 0; i < n; i++)
            for(int j = i; j < n; j++)
                swap(mat[i][j], mat[j][i]);

        for(int i = 0; i < n; i++)
            reverse(mat[i].begin(), mat[i].end());

        return mat;
    }
};
相关推荐
sali-tec5 小时前
C# 基于halcon的视觉工作流-章66 四目匹配
开发语言·人工智能·数码相机·算法·计算机视觉·c#
小明说Java5 小时前
常见排序算法的实现
数据结构·算法·排序算法
行云流水20196 小时前
编程竞赛算法选择:理解时间复杂度提升解题效率
算法
smj2302_796826527 小时前
解决leetcode第3768题.固定长度子数组中的最小逆序对数目
python·算法·leetcode
cynicme7 小时前
力扣3531——统计被覆盖的建筑
算法·leetcode
core5128 小时前
深度解析DeepSeek-R1中GRPO强化学习算法
人工智能·算法·机器学习·deepseek·grpo
mit6.8248 小时前
计数if|
算法
a伊雪8 小时前
c++ 引用参数
c++·算法
圣保罗的大教堂9 小时前
leetcode 3531. 统计被覆盖的建筑 中等
leetcode