日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

48. Rotate Image ~

發(fā)布時(shí)間:2025/4/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 48. Rotate Image ~ 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目:

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 }

別人的答案,聰明人啊,先上下對(duì)稱,再左右對(duì)稱

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 }

類似的,先中心對(duì)稱,再左右對(duì)稱

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 }

這位大哥對(duì)python太了解了,還是寫下來,有些最基本的我看來也不了解

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

A[::-1]是把Python上下反轉(zhuǎn),這個(gè)沒問題,zip()?in conjunction with the?*?operator can be used to unzip a list.

舉例說,是否inplace不討論,不過最后是list of tuple,也有點(diǎn)懷疑啊

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個(gè)list進(jìn)行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])

發(fā)現(xiàn)這位大哥也認(rèn)識(shí)到了這點(diǎn),這個(gè)算法解決了這個(gè)問題, map等于將A當(dāng)中每個(gè)tuple改成了list

map(function,?iterable,?...)

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

更多討論:

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

轉(zhuǎn)載于:https://www.cnblogs.com/panini/p/6718064.html

總結(jié)

以上是生活随笔為你收集整理的48. Rotate Image ~的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。