成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 48. Rotate Image

Warren / 2759人閱讀

Problem

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

Example 1:

Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],

rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
Example 2:

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

rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]

Solution Transpose + Flip horizontally
class Solution {
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n/2; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][n-1-j];
                matrix[i][n-1-j] = temp;
            }
        }
    }
}
4-way swap
class Solution {
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        for (int i = 0; i < n/2; i++) {
            for (int j = i; j < n-i-1; j++) {
                swap(matrix, n, i, j);
            }
        }
    }
    private void swap(int[][] matrix, int n, int i, int j) {
        int temp = matrix[i][j];
        matrix[i][j] = matrix[n-1-j][i];
        matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
        matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
        matrix[j][n-1-i] = temp;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/72651.html

相關(guān)文章

  • LeetCode[48] Rotate Image

    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? 復(fù)雜度O(N^2),O(1) 代碼 public void ro...

    sanyang 評(píng)論0 收藏0
  • leetcode 48 Rotate Image

    摘要:題目詳情這道題目要求我們對(duì)一個(gè)正方形矩陣進(jìn)行順時(shí)針度的翻轉(zhuǎn)。并且要求不聲明額外的空間,不能新建二維數(shù)組。輸入數(shù)組旋轉(zhuǎn)后的輸入數(shù)組想法這道題因?yàn)橐笤谖?。所以我們需要找到一種解法,使得每次操作都是交換兩個(gè)元素的位置,最后實(shí)現(xiàn)整個(gè)矩陣的旋轉(zhuǎn)。 題目詳情 You are given an n x n 2D matrix representing an image.Rotate the ima...

    kgbook 評(píng)論0 收藏0
  • leetcode48 Rotate Image 90度旋轉(zhuǎn)數(shù)組

    摘要:每一次的旋轉(zhuǎn),其實(shí)都是正方形上的四個(gè)元素之間的相互替換。所以本質(zhì)上我們只需遍歷每種長(zhǎng)度正方形上的一條邊,就可以完成這個(gè)正方形的旋轉(zhuǎn)。最后實(shí)現(xiàn)整個(gè)數(shù)組矩陣的旋轉(zhuǎn)代表正方形的起始位置,即,,即,代表當(dāng)前正方形上的一條邊上的一個(gè)點(diǎn)。 題目要求 You are given an n x n 2D matrix representing an image. Rotate the image b...

    melody_lql 評(píng)論0 收藏0
  • leetcode 部分解答索引(持續(xù)更新~)

    摘要:前言從開始寫相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒有按順序?qū)懍F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個(gè)索引嘻嘻。順序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 從開始寫leetcode相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒有按順序?qū)憽F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個(gè)索引嘻嘻。 順序整理 1~50 1...

    leo108 評(píng)論0 收藏0
  • [Leetcode] Rotate Image 旋轉(zhuǎn)圖片

    摘要:交換法復(fù)雜度時(shí)間空間思路為了實(shí)現(xiàn)這題,我們要用交換的方法,順序是左上先和左下交換,然后左上和右下交換,然后左上和右上交換。和類似,我們通過圈數(shù)來控制內(nèi)外的順序。代碼計(jì)算圈數(shù)左上和左下交換左上和右下交換左上和右上交換 Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image...

    Brenner 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<