zoukankan      html  css  js  c++  java
  • [LeetCode 48] Rotate Image

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

    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 rotation.

     

    Example 1:

    Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
    Output: [[7,4,1],[8,5,2],[9,6,3]]
    

    Example 2:

    Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
    Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
    

    Example 3:

    Input: matrix = [[1]]
    Output: [[1]]
    

    Example 4:

    Input: matrix = [[1,2],[3,4]]
    Output: [[3,1],[4,2]]
    

     

    Constraints:

    • matrix.length == n
    • matrix[i].length == n
    • 1 <= n <= 20
    • -1000 <= matrix[i][j] <= 1000

     

    Solution 1. O(n^2) runtime, O(n^2) space

     1 public class Solution {
     2     public void rotate(int[][] matrix) {
     3         if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
     4             return;
     5         }
     6         int n = matrix.length;
     7         int[][] rotate = new int[n][n];
     8         for(int i = 0; i < n; i++){
     9             for(int j = 0; j < n; j++){
    10                 rotate[i][j] = matrix[n - 1 - j][i];
    11             }
    12         }
    13         for(int i = 0; i < n; i++){
    14             for(int j = 0; j < n; j++){
    15                 matrix[i][j] = rotate[i][j];
    16             }
    17         }
    18         //matrix = rotate;
    19     }
    20 }

    One pitfall here is that line 18 does not change the original matrix that needs to be rotated. Java is pass-by-copy, so a copy of matrix, which is 

    a reference to the actual memory location of the 2D matrix, is passed to method rotate. You should use this copy of reference to change the 

    actual matrix entries. Changing this copy of reference to a new location does not touch the original matrix at all. 

     

    Solution 2. O(n^2) runtime, O(1) space 

    The runtime in solution 1 is already BCR asymptotically. But we can still optimize on extra memory usage.

    Perform a circular rotation on each layer,(left -> top,  bottom -> left, right -> bottom, top -> bottom. ),

    starting from the outermost layer and working our way inwards.

     

    How do we perform this 4-way edge swap? One option is to copy the top edge to an array, then move 

    the left to the top, the bottom to the left, and so on. This requires O(n) memory. 

    A better way to do this is to implement the swap index by index, so we only needs O(1) extra memory. 

     

     1 public class Solution {
     2     public void rotate(int[][] matrix) {
     3         if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
     4             return;
     5         }
     6         int n = matrix.length;
     7         for(int layer = 0; layer < n / 2; layer++){
     8             int first = layer;
     9             int last = n - 1 - layer;
    10             for(int i = first; i < last; i++){
    11                 int offset = i - first;
    12                 //save top
    13                 int top = matrix[first][i];
    14                 //left -> top
    15                 matrix[first][i] = matrix[last - offset][first];
    16                 //bottom -> left
    17                 matrix[last - offset][first] = matrix[last][last - offset];
    18                 //right -> bottom
    19                 matrix[last][last - offset] = matrix[i][last];
    20                 //top -> right
    21                 matrix[i][last] = top;
    22             }
    23         }
    24     }
    25 }

     

     

    Related Problems

    Rotate String 

    Matrix Zigzag Traversal

    Set Matrix Zeroes

  • 相关阅读:
    Django-website 程序案例系列-3 URL详解
    Django-website 程序案例系列-1 最简单的web服务器
    c# 多维数组、交错数组(转化为DataTable)
    c++(重载等号=操作为深拷贝)
    c# 导入c++ dll
    Nhibernate HQL 匿名类(严格说是map的使用以及构造函数的使用
    spring.net 集成nhibernate配置文件(这里暴露了GetCurrentSession 对于 CurrentSession unbond thread这里给出了解决方法)
    hibernate mapping文件中 xmlns会导致linq to xml 查询不到对应的节点
    linq to xml
    xml 操作(动态添加 property属性 其他节点同理)
  • 原文地址:https://www.cnblogs.com/lz87/p/7056900.html
Copyright © 2011-2022 走看看