一天一道LeetCode系列

(一)题目

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?

(二)解题

90度旋转图像,我们不难看出

经过这样的变换后,图像就旋转了90度。



class Solution {

public:

    void rotate(vector<vector<int>>& matrix) {

        int n = matrix.size()-1;

        vector<vector<int>> tmp = matrix;//深拷贝

        for(int i = 0 ; i< n+1; i++)

        {

            for(int j = 0 ; j < n+1 ; j++)

            {

                matrix[j][n-i] = tmp[i][j];//赋值转换

            }

        }

    }

};

网上还有一种解法去先转置在对称变换。代码如下:


class Solution {

public:

    void rotate(vector<vector<int> > &matrix) {

        int dim = matrix.size();

        int temp = 0;

        for (int i = 0; i < dim; ++i) { //先转置

            for (int j = i+1; j < dim; ++j) {

                temp = matrix[i][j];

                matrix[i][j] = matrix[j][i];

                matrix[j][i] = temp;

            }

        }

        for (int i = 0; i < dim/2; ++i) { //然后对称变换

            for (int j = 0; j < dim; ++j) {

                temp = matrix[j][i];

                matrix[j][i] = matrix[j][dim - i -1];

                matrix[j][dim - i -1] = temp;

            }

        }

    }

};

【一天一道LeetCode】#48. Rotate Image的更多相关文章

  1. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  2. 每日一道 LeetCode (48):最长回文子串

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  3. [LeetCode] 48. Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  4. LeetCode 48. Rotate Image(旋转图像)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  5. LeetCode 48 Rotate Image(2D图像旋转问题)

    题目链接: https://leetcode.com/problems/rotate-image/?tab=Description   Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...

  6. [leetcode 48] rotate image

    1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...

  7. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  8. [leetcode]48. Rotate Image旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  9. LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)

    题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazi ...

  10. LeetCode 48. Rotate Image (C++)

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

随机推荐

  1. Docker 工具和示例

    pipework Jérôme Petazzoni 编写了一个叫 pipework 的 shell 脚本,可以帮助用户在比较复杂的场景中完成容器的连接. playground Brandon Rhod ...

  2. iOS图形手势识别框架SGGestureRecognizer

    简介 苹果官方为我们提供了简单手势的识别器,但对于图形手势,例如五角星.三角形等的识别,就需要自己实现了.通过识别这些手势,可以去执行特定的操作,或是输入公式.释放魔法等,可以为App增光添彩. 下载 ...

  3. Linux下端口复用(SO_REUSEADDR与SO_REUSEPORT)

    freebsd与linux下bind系统调用小结:    只考虑AF_INET的情况(同一端口指ip地址与端口号都相同) freebsd支持SO_REUSEPORT和SO_REUSEADDR选项,而l ...

  4. C算法实现:将字符串中的数字返回为整型数

    今天看linux内核驱动的代码,发现一个算法写得挺简单,也有意思. 分享一下我的测试代码: #include <stdio.h> typedef int U32 ; U32 String2 ...

  5. Android Multimedia框架总结(十八)Camera2框架从Java层到C++层类关系

    Agenda: getSystemService(Context.CAMERA_SERVICE) CameraManager.getCameraIdList() ICameraService.aidl ...

  6. Android Studio 不得不知的20大快捷键

    如何进入设置快捷键的界面: Android Studio -> References -> Keymap 使用的Keymaps为 Eclipse(Mac OS X)如下图所示: 1 展开和 ...

  7. Git 解决一个电脑多用户情况(win7)

    首先:在输入ssh-keygen -t rsa -C "注册邮箱"后不要急着按enter,此时输入秘钥对的文件名,不要跟默认文件重名(默认的是id_rsa) 划红线的地方就是新的文 ...

  8. FFmpeg源代码简单分析:av_write_frame()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  9. Dynamics Crm 2011 Or 2013 IFD 部署一段时间后,CA验证问题

    以下错误描述摘自博客:http://blog.csdn.net/qzw4549689/article/details/14451257 IFD部署一段时间后,大概一年,突然出现从IFD登录页面登录后, ...

  10. Android 四种常见的线程池

    引入线程池的好处 1)提升性能.创建和消耗对象费时费CPU资源 2)防止内存过度消耗.控制活动线程的数量,防止并发线程过多. 我们来看一下线程池的简单的构造 public ThreadPoolExec ...