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?

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

4/15/2017

4ms, 8%

 1 public class Solution {
 2     public void rotate(int[][] matrix) {
 3         int length = matrix.length;
 4         for (int i = 0; i < (length + 1) / 2; i++) {
 5             for (int j = i; j < length - 1 - i; j++) {
 6                 int tmp = matrix[i][j];
 7                 matrix[i][j] = matrix[length - 1 - j][i];
 8                 matrix[length - 1 - j][i] = matrix[length - 1 - i][length - 1 - j];
 9                 matrix[length - 1 - i][length - 1 - j] = matrix[j][length - 1 - i];
10                 matrix[j][length - 1 - i] = tmp;
11             }
12         }
13         return;
14     }
15 }

别人的答案,聪明人啊,先上下对称,再左右对称

https://discuss.leetcode.com/topic/6796/a-common-method-to-rotate-the-image

 1 /*
 2  * clockwise rotate
 3  * first reverse up to down, then swap the symmetry 
 4  * 1 2 3     7 8 9     7 4 1
 5  * 4 5 6  => 4 5 6  => 8 5 2
 6  * 7 8 9     1 2 3     9 6 3
 7 */
 8 void rotate(vector<vector<int> > &matrix) {
 9     reverse(matrix.begin(), matrix.end());
10     for (int i = 0; i < matrix.size(); ++i) {
11         for (int j = i + 1; j < matrix[i].size(); ++j)
12             swap(matrix[i][j], matrix[j][i]);
13     }
14 }

类似的,先中心对称,再左右对称

https://discuss.leetcode.com/topic/9744/ac-java-in-place-solution-with-explanation-easy-to-understand

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

这位大哥对python太了解了,还是写下来,有些最基本的我看来也不了解

https://discuss.leetcode.com/topic/15295/seven-short-solutions-1-to-7-lines

A[::-1]是把Python上下反转,这个没问题,zip() in conjunction with the * operator can be used to unzip a list.

举例说,是否inplace不讨论,不过最后是list of tuple,也有点怀疑啊

A = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]
A[::-1] = [[13, 14, 15, 16], [9, 10, 11, 12], [5, 6, 7, 8], [1, 2, 3, 4]]
zip(*A[::-1])等于是把4个list进行unzip,得
[(13, 9, 5, 1), (14, 10, 6, 2), (15, 11, 7, 3), (16, 12, 8, 4)]
class Solution:
    def rotate(self, A):
        A[:] = zip(*A[::-1])

发现这位大哥也认识到了这点,这个算法解决了这个问题, map等于将A当中每个tuple改成了list

map(functioniterable...)

class Solution:
    def rotate(self, A):
        A[:] = map(list, zip(*A[::-1]))

更多讨论:

https://discuss.leetcode.com/category/56/rotate-image

posted @ 2017-04-16 10:16  panini  阅读(104)  评论(0编辑  收藏  举报