LeetCode48/189 Rotate Image/Rotate Array

一: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?

链接:https://leetcode.com/problems/rotate-image/

分析:这里我提供两种解法

法一:需要额外的空间复杂度O(MN),时间复杂度为O(MN),旋转90度就是将[i,j]放在新数组的[j, n-1-i]位置处

       int n = matrix.size();
       vector<vector<int> > tmp = matrix;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                matrix[j][n-i-1] = tmp[i][j];
            }
        }

法二:将一副图像旋转90度就是将其上下对折,然后再按照对角线对折,It is amazing!!时间复杂度仍然为O(MN),但不需要额外的空间了。

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        int n = matrix.size();
       /* vector<vector<int> > tmp = matrix;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                matrix[j][n-i-1] = tmp[i][j];
            }
        }*/
        for(int i = 0; i < n; i++){  // 先上下对折
            for(int j = 0; j < n/2; j++)
                swap(matrix[i][j], matrix[i][n-1-j]);
        }

        for(int i = 0; i < n; i++){   // 后按照对角线对折就能顺时针旋转90度了
            for(int j = 0; j < n-1-i; j++)
                swap(matrix[i][j], matrix[n-1-j][n-i-1]);
        }
    }
};

二:Rotate Array

题目:

Rotate an array of n elements to the right by k steps.

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is
rotated to [5,6,7,1,2,3,4].

链接:https://leetcode.com/problems/rotate-array/

分析:这里仍提供两种方法,法一需要额外的空间O(N),法二利用对折就可以了,方法是先对折前面n-k个元素,然后对折后面k个元素,最后对折这n个元素就可以了,it is amazing!!

class Solution {
public:
    void rotate(int nums[], int n, int k) {
       /* k = k%n;
        int *a = new int[k];    // 需要空间复杂度O(k)
        for(int i = 0; i < k; i++)
            a[i] = nums[n-k+i];
        for(int i = n-k-1; i >= 0; i--)
            nums[i+k] = nums[i];
        for(int i = 0; i < k; i++)
            nums[i] = a[i];
        delete []a;*/
        k = k % n;
        for(int i = 0; i < (n-k)/2; i++)    // 先对折前面的n-k个元素
            swap(nums[i], nums[n-k-1-i]);
        for(int i = 0; i < k/2; i++)
            swap(nums[n-k+i], nums[n-1-i]);   // 对折后面的k个元素
        for(int i = 0; i < n/2; i++)
            swap(nums[i], nums[n-1-i]);       // 最后对折这n个元素
    }
};
时间: 2024-08-27 09:14:21

LeetCode48/189 Rotate Image/Rotate Array的相关文章

Jan 17 - Rotate Image; 2D Array; xPos and yPos;

代码: public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for(int i = 0; i < n/2; i++){ int num = n-i*2; for(int j = 0; j < num-1; j++){ int temp = matrix[i][i+j]; matrix[i][i+j] = matrix[i+num-1-j][i]; matrix[i+num-1-j

[LeetCode] Rotate Array / List

Question: http://leetcode.com/2010/04/rotating-array-in-place.html Rotate a one-dimensional array of n elements to the right by k steps.  For instance, with n=7 and k=3, the array {a, b, c, d, e, f, g} is rotated to {e, f, g, a, b, c, d} // O (n) pub

Rotate Array 解答

Question Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Solution 1 Bubble Rotate Similar with bubble sort, time complexity O(k * n), space cost O(1) 1

变形--旋转 rotate()

变形--旋转 rotate() 旋转rotate()函数通过指定的角度参数使元素相对原点进行旋转.它主要在二维空间内进行操作,设置一个角度值,用来指定旋转的幅度.如果这个值为正值,元素相对原点中心顺时针旋转:如果这个值为负值,元素相对原点中心逆时针旋转.如下图所示: HTML代码: <div class="wrapper"> <div></div> </div> CSS代码: .wrapper { width: 200px; height

STL 源码分析  之 rotate()函数分析

rotate()函数究竟能干嘛? http://www.cplusplus.com/reference/algorithm/rotate/?kw=rotate 上面只是大致的截图, 源码在下面给出: // rotate and rotate_copy, and their auxiliary functions template <class _EuclideanRingElement> _EuclideanRingElement __gcd(_EuclideanRingElement __m

CSS3 2D 转换【旋转transform:rotate(30deg); 移动transform: translate(50px,100px); 放大缩小transform:scale(2,4)】

CSS3 2D 转换 CSS3 转换 CSS3转换,我们可以移动,比例化,反过来,旋转,和拉伸元素. 它是如何工作? 变换的效果,让某个元素改变形状,大小和位置. 您可以转换您使用2D或3D元素. 浏览器支持 表格中的数字表示支持该属性的第一个浏览器版本号. 紧跟在 -webkit-, -ms- 或 -moz- 前的数字为支持该前缀属性的第一个浏览器版本号. Property           transform 36.0 4.0 -webkit- 10.0 9.0 -ms- 16.0 3.5

【技术分享】sphinx的--rotate运行机制

如果sphinx在运行中,要indexer时,需要加上--rotate参数,这样索引完就直接生效了. 原因是sphinx的searchd在启动时会创建一个.sql锁文件,因为这时已经标志sphinx正在运行中,除非使用--rotate. rotate运行机制 ->indexer完成索引 ->发送SIGHUP给searchd(同时在终端输出索引已经 完成) ->searchd接到中断信号->等待所有子进程退出 ->重命名 当前索引为旧索引为.old ->重命名 .new索

jQuery转盘插件rotate

css .rotate{ background:#aaa; padding:100px; position: relative; } .point { position: absolute; top: 215px; left: 270px; width: 149px; height: 200px; background: url(http://fundact.eastmoney.com/app4/images/luckyPoint.png) no-repeat; } .luckyBtn{ pos

Rotating an array in place

April 13, 2010 in Uncategorized Rotate a one-dimensional array of n elements to the right by k steps. For instance, with n=7 and k=3, the array {a, b, c, d, e, f, g} is rotated to {e, f, g, a, b, c, d}. In my previous post we discussed about finding