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 rotate(int[][] matrix) { int n = matrix.length; for(int layer = 0; layer < n / 2; layer ++) { int start = layer; int end = n - 1 - start; for(int i = start; i < end; i ++) { int offset = i - start; int temp = matrix[start][i]; //left to top; matrix[start][i] = matrix[end - offset][start]; //bottom to left; matrix[end - offset][start] = matrix[end][end - offset]; //right to bottm; matrix[end][end - offset] = matrix[i][end]; //top to right; matrix[i][end] = temp; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/65293.html
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 mat...
摘要:題目詳情這道題目要求我們對一個正方形矩陣進行順時針度的翻轉(zhuǎn)。并且要求不聲明額外的空間,不能新建二維數(shù)組。輸入數(shù)組旋轉(zhuǎn)后的輸入數(shù)組想法這道題因為要求在位。所以我們需要找到一種解法,使得每次操作都是交換兩個元素的位置,最后實現(xiàn)整個矩陣的旋轉(zhuǎn)。 題目詳情 You are given an n x n 2D matrix representing an image.Rotate the ima...
摘要:每一次的旋轉(zhuǎn),其實都是正方形上的四個元素之間的相互替換。所以本質(zhì)上我們只需遍歷每種長度正方形上的一條邊,就可以完成這個正方形的旋轉(zhuǎn)。最后實現(xiàn)整個數(shù)組矩陣的旋轉(zhuǎn)代表正方形的起始位置,即,,即,代表當(dāng)前正方形上的一條邊上的一個點。 題目要求 You are given an n x n 2D matrix representing an image. Rotate the image b...
摘要:前言從開始寫相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時也沒有按順序?qū)懍F(xiàn)在翻起來覺得蠻亂的。可能大家看著也非常不方便。所以在這里做個索引嘻嘻。順序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 從開始寫leetcode相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時也沒有按順序?qū)憽F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個索引嘻嘻。 順序整理 1~50 1...
摘要:交換法復(fù)雜度時間空間思路為了實現(xiàn)這題,我們要用交換的方法,順序是左上先和左下交換,然后左上和右下交換,然后左上和右上交換。和類似,我們通過圈數(shù)來控制內(nèi)外的順序。代碼計算圈數(shù)左上和左下交換左上和右下交換左上和右上交換 Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image...
閱讀 3710·2021-11-24 10:22
閱讀 3787·2021-11-22 09:34
閱讀 2657·2021-11-15 11:39
閱讀 1584·2021-10-14 09:42
閱讀 3716·2021-10-08 10:04
閱讀 1617·2019-08-30 15:52
閱讀 924·2019-08-30 13:49
閱讀 3079·2019-08-30 11:21