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

题目大意:给一个矩阵,将其按顺时针旋转90°。

题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置。I just want to say"It‘s amazing!".(forgivig my poor English!)

代码如下(代码怎么写已经不重要了!):

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int r=matrix.size();
        int c=matrix[0].size();
        for(int i=0;i<r;++i){
            for(int j=i+1;j<c;++j)
                swap(matrix[i][j],matrix[j][i]);
            for(int j=0,k=c-1;j<k;++j,--k)
                swap(matrix[i][j],matrix[i][k]);
        }
    }
};

  

时间: 2024-08-09 02:04:38

LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)的相关文章

LeetCode 48. 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? 题目标签:Array 这道题目给了我们一个n * n的矩阵,让我们旋转图片.题目要求in-place,所以就不能用额外的空间了.一开始自己写了一个很原始的方法,结果虽然通过,但是速度太慢.只好去看看别人的方法,看完觉得,自己

Leetcode 48 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? 思路:把矩阵看成是由多个正方形一层一层包围起来的.转动的时候就是转动正方形的边,只要把边上的每个数依次替换就好,四个方向也就是进行四次替换,依次对每一层进行替换. 总层数 = matrix.length/2,第i层从第mat

LeetCode 48. Rotate Image (C++)

题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do

19.2.7 [LeetCode 48] Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the

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

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? 思路:其实就是旋转数组,没有什么难度,代码如下: public class Solution { public void rotate(int[][] matrix) { int[][] a =

[LeetCode] 61. Rotate List 旋转链表

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g

2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1363    Accepted Submission(s): 717 Problem Description Sudoku i

LeetCode --- 61. Rotate List

题目链接:Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 这道题的要求是向右旋转链表k步. 其实就是把链表后面l-k个节点放到前面,可以采用快慢指针处理.不

【Leetcode】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? 思路:第一种思路是一层一层的进行旋转,比较直观:第二种思路则比较取巧,首先沿着副对角线翻转一次,然后沿着水平中线翻转一次. 代码一: class Solution { public: void rotate(vector<