LeetCode 048 Rotate Image

Stella981
• 阅读 529

题目要求:Rotate Image

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?

LeetCode 048 Rotate Image

代码如下:

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        
        const int n = matrix.size();
        
        for(int i = 0; i < n; i++)  //沿着副对角线反转
            for(int j = 0; j < n - i; j++)
                swap(matrix[i][j], matrix[n - 1 - j][n - 1 - i]);
                
        for(int i = 0; i < n / 2; i++)//沿着水平中线反转
            for(int j = 0; j < n; j++)
                swap(matrix[i][j], matrix[n - 1 - i][j]);
        
    }
};

本文同步分享在 博客“Yano_nankai”(CNBlog)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Stella981 Stella981
2年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Stella981 Stella981
2年前
Node.js 12中的ES模块[每日前端夜话0x9E]
每日前端夜话0x9E每日前端夜话,陪你聊前端。每天晚上18:00准时推送。正文共:2552字预计阅读时间:10 分钟作者:BrianDeSousa翻译:疯狂的技术宅来源:logrocket!(https://oscimg.oschina.net/oscnet/2ccaf94cecd3
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Easter79 Easter79
2年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Stella981 Stella981
2年前
279. 完全平方数 leetcode JAVA
题目:给定正整数 _n_,找到若干个完全平方数(比如 1,4,9,16,...)使得它们的和等于_n_。你需要让组成和的完全平方数的个数最少。示例 1:输入:_n_12输出:3解释:12444.示例2:输入:_n_13输出:2解释:134
Stella981 Stella981
2年前
LeetCode 92. 反转链表 II(Reverse Linked List II)
题目描述反转从位置_m_到_n_的链表。请使用一趟扫描完成反转。说明:1≤ _m_ ≤ _n_ ≤链表长度。示例:输入:12345NULL,m2,n4输出:14325NULL解题思路本题类似于反转链表
Stella981 Stella981
2年前
CodeForces 327E Axis Walking(状压DP+卡常技巧)
IahubwantstomeethisgirlfriendIahubina.Theybothlivein _Ox_ axis(thehorizontalaxis).Iahublivesatpoint0andIahubinaatpoint _d_.Iahubhas _n_ positiveinte
Wesley13 Wesley13
2年前
67,盛最多水的容器
给定 _n_ 个非负整数 _a_1,_a_2,...,_a_n,每个数代表坐标中的一个点 (_i_, _ai_)。在坐标内画 _n_ 条垂直线,垂直线 _i_ 的两个端点分别为 (_i_, _ai_)和(_i_,0)。找出其中的两条线,使得它们与 _x_ 轴共同构成的容器可以容纳最多的水。说明:你不能倾斜容器,且 _n_ 的值至少为2。!
Stella981 Stella981
2年前
LeetCode(48):旋转图像
Medium!题目描述:给定一个 _n_× _n_ 的二维矩阵表示一个图像。将图像顺时针旋转90度。说明:你必须在原地(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fbaike.baidu.com%2Fitem%2F%25E5%258E%259