1. 程式人生 > >【旋轉矩陣】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?

原地選擇矩陣,將矩陣一圈一圈的原地旋轉
public class Solution {
	    public void rotate(int[][] matrix) {
	        if(matrix == null) return ;
	        int rows = matrix.length;
	        if(rows <= 1) return;
	        
	        for(int left=0, right=rows-1; left<right; left++, right--){
	        	for(int high=left, low=right; high<low; high++, low--){
	        		int t = matrix[high][left];
	        		matrix[high][left] = matrix[low][left];
	        		matrix[low][left] = matrix[low][right];
	        		matrix[low][right] = matrix[high][right];
	        		matrix[high][right] = t;
	        	}
	        }
	    }
	}




相關推薦

旋轉矩陣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-p

LeetCodePython題解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-pl

poj 2187旋轉卡殼

getch getchar() Go char tor pre -a str clu 求平面最遠點對 #include<iostream> #include<cstdio> #include<algorithm> #include<

LeetCode : 48. 旋轉影象(Rotate Image)解答

48. 旋轉影象 給定一個 n × n 的二維矩陣表示一個影象。 將影象順時針旋轉 90 度。 說明: 你必須在 原地 旋轉影象,這意味著你需要直接修改輸入的二維矩陣。請不要 使用另一個矩陣來旋轉影象。 示例 1: 給定 matrix = [  

KMP+矩陣BZOJ

題意: 給出一串長度為m的不吉利數字,要構造出一個長度為n的數字串不包含這個不吉利的數字,問構造方案數是多少。 (m<=20,n<=1e9) 題解: 設陣列dp[i][j]表

KMP+矩陣BZOJ - 1009 - GT考試

題目連結<https://cn.vjudge.net/problem/HYSBZ-1009> 題意: 給出一串長度為m的不吉利數字,要構造出一個長度為n的數字串不包含這個不吉利的數字,問構造方案數是多少。 (m<=20,n<=1e9) 題解:

矩陣高效演算法設計

.子矩陣 小A 有一個N×M 的矩陣,矩陣中1~N*M 這(N*M)個整數均出現過一次。現在小A 在這個矩陣內選擇一個子矩陣,其權值等於這個子矩陣中的所有數的最小值。小A 想知道,如果他選擇的子矩陣的

圖的遍歷DFS鄰接矩陣

對於一個圖,頂點與頂點之間如果相連的話,是沒有權值的 但是對於一個網則不同,2個頂點如果有聯絡,那就需要有權值 #include<cstdio> int visited[1000];

論文閱讀Accurate Image Super-Resolution Using Very Deep Convolutional Networks

開發十年,就只剩下這套架構體系了! >>>   

leetcode 48. 旋轉圖像(Rotate Image)

++ leetcode .com 題目 順時針 http baidu https leet 目錄 題目描述: 示例 1: 示例 2: 解法:

leetcode#陣列Python48. Rotate Image 旋轉影象

連結: 題目: 給定一個 n × n 的二維矩陣表示一個影象。 將影象順時針旋轉 90 度。 說明: 你必須在原地旋轉影象,這意味著你需要直接修改輸入的二維矩陣。請不要使用另一個矩陣來旋轉影象。 示

LeetCode048. Rotate Image

swap cat int directly inpu 對角線 leet bsp log 題目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (c

LeetCode題解61_旋轉連結串列(Rotate-List)

目錄 描述 解法:雙指標 思路 Java 實現 Python 實現 複雜度分析 描述 給定一個連結串列,旋轉連結串列,將連結串列每個節點向右移動 k 個位置,其中 k 是非負數。 示例 1: 輸入: 1->2->3->4-

LeetCode題解61_旋轉鏈表(Rotate-List)

__init__ span leetcode 我們 分享 圖片 表示 執行 elf 目錄 描述 解法:雙指針 思路 Java 實現 Python 實現 復雜度分析 描述 給定一個鏈表,旋轉鏈表,將鏈表每個節點向右移動 k 個位置,其中 k 是非負數。 示例 1: 輸入

leetcode48.(Medium)Rotate Image

題目連結 解題思路: 首先如果二維陣列是11或者22,直接蠻力計算; 如果是22以上,可以一圈一圈(rotate circle)地計算反轉90度之後的結果。 比如,對於下面55的陣列: 首先設定一個數組tmp將框1中的數字儲存起來, 然後計算翻轉後框1(top layer)中的

LeetCode48. Rotate Image(C++)

地址:https://leetcode.com/problems/rotate-image/ 題目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (c

#138-(EZOI模擬賽)數學技巧旋轉矩陣

Description 一個n行n列的螺旋矩陣可由如下方法生成: 從矩陣的左上角(第1行第1列)出發,初始時向右移動;如果前方是未曾經過的格子,則繼續前進,否則右轉;重複上述操作直至經過矩陣中所有格

LeetCodeRotate Array 旋轉陣列

題目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7]

矩陣順時針旋轉90度 leetCode:Rotate Image

一個影象用矩陣表示,將這個影象順時針旋轉90度,也就是將矩陣順時針旋轉90度。演算法要求不使用額外的空間複雜度,in-place演算法。 觀察了一下矩陣的轉置,發現可以通過以此交換矩陣兩行,然後再求轉置的方式獲得。思路如下: 1、設矩陣有n行,則分別將i=0.1....(n

LeetCode每天一題Rotate List(旋轉鏈表)

type etc exp 直接 style info back 結果 null Given a linked list, rotate the list to the right by k places, where k is non-negative. Example