`
huntfor
  • 浏览: 195494 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[leetcode]Rotate Image

 
阅读更多

新博文地址:[leetcode]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?

 我没有想到in-place的方法,因此开辟了O(n^2)的空间,显然是不满足的要求的。

代码如下:

    public void rotate(int[][] matrix) {
        int n = matrix.length;
        if(n <= 1){
            return;
        }
        int[][] tem = new int[n][n];
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                tem[j][n - 1 - i] = matrix[i][j];
            }
        }
        matrix = tem;//这里错了
    }

 这个答案是WA的,具体原因涉及到了java中值传递和引用传递的知识,大家可以看这篇小结

因此,我们需要人为的将matrix进行赋值:

    public void rotate(int[][] matrix) {
        int n = matrix.length;
        if(n <= 1){
            return;
        }
        int[][] tem = new int[n][n];
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                tem[j][n - 1 - i] = matrix[i][j];
            }
        }
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                matrix[i][j] = tem[i][j];
            }
        }
    }

 这样就顺利AC了,不过不满足题中所说的in-place条件,第二遍再优化吧。新博文中采用了in-place的方法,请移步[leetcode]Rotate Image

分享到:
评论

相关推荐

    LeetCode最全代码

    # [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...

    leetcode答案-LeetCode:LeetCode上的题解

    leetcode 答案 LeetCode 该项目是 LeetCode 上的题解。 src/easy/路径下都是难度为 ..._48_RotateImage这个类名。运行效果如下图所示: 把这个类名复制一下,新建类的时候直接把类名粘贴过去就可以了。

    戳气球leetcode-leetcode:leetcode

    leetcode category other hot keywords:Palindrome(mic), Subsequence Array 螺旋矩阵Spiral Matrix 顺时针打印矩阵 Next Permutation Product of Array Except Self 189.rotate-array 283.move-zero Range Sum ...

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 ...Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73

    leetcode1004-leetcode:leetcode

    Rotate Image (M) -&gt; 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E) 653. Two Sum IV - Input is a BST (E) -&gt; 2 26. Remove Duplicates from Sorted Array (E) 27. ...

    leetcode2sumc-LeetCode_py:LeetCode_py

    RotateImage 153 - SpiralMatrix 顺时针旋转矩阵 90 度。 首先翻转矩阵,交换对称153是直接的 01/31/2020 75 - SortColors 167 - TwoSum2 DCP 75 是一个双指针问题,如果当前项为 0,则使用 p1 p2 指向开始和结束,...

    leetcode跳跃-leetcode:leetcode解题之路

    旋转图像](./Array/rotate-image.md) Heap 堆 [0023 合并K个排序链表](./Heap/merge-k-sorted-lists.md) String 字符串 [0006 Z字形变换](./String/zigzag-conversion.md) [0030 串联所有单词的子串](./String/...

    lrucacheleetcode-leetcode:记录自己的leetcode解题历程~Welcomeeveryonetocomment:grinning_face:~

    Rotate Image 344. Reverse String 414. Third Maximum Number 448. Find All Numbers Disappeared in an Array 66. Plus One 238. Product of Array Except Self 697. Degree of an Array 849. Maximize ...

    javalruleetcode-magician:java学习

    java lru leetcode ##Thinking in Java chapter21 ##Netty in Action ####chapter2: echo ...##leetcode ...[Rotate Image] () [Ugly Number] () [Ugly Number II] () [Repeated DNA Sequences] () [Lar

    cpp-算法精粹

    Rotate Image Plus One Climbing Stairs Set Matrix Zeroes Gas Station Candy Majority Element Rotate Array Contains Duplicate Contains Duplicate II Contains Duplicate III Product of Array Except Self ...

Global site tag (gtag.js) - Google Analytics