有一个NxN整数矩阵,请编写一个算法,将矩阵顺时针旋转90度。
给定一个NxN的矩阵,和矩阵的阶数N,请返回旋转后的NxN矩阵。
数据范围:,矩阵中的值满足
要求:空间复杂度 ,时间复杂度
进阶:空间复杂度 ,时间复杂度
//就地处理,空间复杂度为O(1),时间复杂度O(N) class Solution { public: void swap(int &a,int &b) { int temp = a; a = b; b = temp; } vector<vector<int> > rotateMatrix(vector<vector<int> > mat, int n) { // write code here if(n == 1 || n == 0) return mat; int up = 0; int right = n-1; int down = n-1; int left = 0; while(up < down && left < right) { //1,2 swap int begin1 = up + 1; for(int i = left + 1;i < right;i++) { swap(mat[up][i],mat[begin1++][right]); } //2 4 swap begin1 = down - 1; for(int i = left + 1;i < right;i++) { swap(mat[up][i],mat[begin1--][left]); } //3 2 swap begin1 = up + 1; for(int i = left + 1;i < right;i++) { swap(mat[down][i],mat[begin1++][left]); } swap(mat[up][left],mat[up][right]); swap(mat[up][left],mat[down][left]); swap(mat[down][left],mat[down][right]); up++; down--; left++; right--; } return mat; } };
class Solution { public: vector<vector<int>> rotateMatrix(vector<vector<int>> mat, int n) { // write code here for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) swap(mat[i][j], mat[j][i]); for (int i = 0; i < n; i++) for (int j = 0; j < n / 2; j++) swap(mat[i][j], mat[i][n - 1 - j]); return mat; } static void swap(int &a, int &b) { int temp = a; a = b; b = temp; } };
新建一个数组,把这个数组按行导入另外一个数组的列中,只不过这个列从大到小就行了
class Solution { public: vector > rotateMatrix(vector > mat, int n) { vector> transMat; vector matCol; for(int i=0;i<mat[0].size();++i){ for(int i=0;i<mat.size();++i){ matCol.push_back(0); } transMat.push_back(matCol); matCol.clear(); } int i=0,j=0,k=n-1,l=0; for(;i<n;++i){ l = 0; for(j=0;j<n;++j){ transMat[l++][k]=mat[i][j]; } --k; } return transMat; } };
class Solution { public: vector<vector<int> > rotateMatrix(vector<vector<int> > mat, int n) { // write code here for(int i = 0 ; i < n/2; i++){ for(int j = 0 ; j < n; j++){ swap(mat[i][j],mat[n-1-i][j]); } } for(int i = 0 ; i < n;i++){ for(int j = i + 1; j < n;j++){ swap(mat[i][j],mat[j][i]); } } return mat; } };我是安徽土木学院猪贲,带你刷穿牛客
import java.util.*; public class Solution { /* * 两次对折 * ①沿着横轴进行一次对折 * 沿着主对角线进行一次对折 */ public int[][] rotateMatrix(int[][] mat, int n) { // write code here // 第一次对折 int i = 0; int j = n-1; while(i < j){ for(int k=0;k<n;k++){ int tmp = mat[i][k]; mat[i][k] = mat[j][k]; mat[j][k] = tmp; } i++; j--; } // 第二次对折 for(int k=0;k<n;k++){ for(int m=0;m<k;m++){ int tmp = mat[k][m]; mat[k][m] = mat[m][k]; mat[m][k] = tmp; } } return mat; } }
class Solution { public: vector<vector<int> > rotateMatrix(vector<vector<int> > mat, int n) { // transpose matrix (逆置矩阵) for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) swap(mat[i][j], mat[j][i]); // reverse for (auto& row : mat) std::reverse(begin(row), end(row)); return mat; } };
import java.util.*; public class Solution { public int[][] rotateMatrix(int[][] mat, int n) { // 上下交换 for(int i = 0; i <= (n - 1) / 2; i++){ swap(mat, i, n - 1 - i); } // 斜对角交换 for(int i = 0; i < n ; i++){ for(int j = 0; j < i; j++){ int temp = mat[i][j]; mat[i][j] = mat[j][i]; mat[j][i] = temp; } } return mat; } public void swap(int[][] nums, int i, int j){ int[] temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } }
import java.util.*; public class Solution { public int[][] rotateMatrix(int[][] mat, int n) { // write code here /* */ int[][] ans=new int[mat[0].length][mat.length]; for(int i=0;i<ans.length;i++){ for(int j=0;j<ans[0].length;j++){ ans[i][j]=mat[ans.length-1-j][i]; } } return ans; } }
import java.util.*; public class Solution { public int[][] rotateMatrix(int[][] mat, int n) { // write code here int [][]a=new int [mat[0].length][mat.length]; for(int i=0;i<a.length;i++){ for(int j=0;j<a[0].length;j++){ a[i][j]=mat[a[0].length-j-1][i]; } } return a; } }
//首先想到直接旋转,但是一层一层超级慢 //所以想到先沿对角线翻转,再沿水平中线翻转 class Solution { public: vector<vector<int> > rotateMatrix(vector<vector<int> > mat, int n) { // write code here // 沿对角线翻转(左下角到右上角的对角线) for (int i = 0; i < n; ++i) { for (int j = 0; j < n -i; ++j) { swap(mat[i][j], mat[n - 1 - j][n - 1 - i]); } } // 沿水平中线翻转 for (int i = 0; i < n / 2; ++i) { for (int j = 0; j < n; ++j) { swap(mat[i][j], mat[n - 1 - i][j]); } } return mat; } };
import java.util.*; public class Solution { public int[][] rotateMatrix(int[][] mat, int n) { // write code here int[][] newmat = new int[n][n]; for (int i=0;i<n;i++) { for (int j=0;j<n;j++) { newmat[i][j] = mat[n-j-1][i]; } } return newmat; } }
public int[][] rotateMatrix(int[][] mat, int n) { // write code here if(n>500) return null; int[][] temp=new int[n][n]; for ( int i = 0; i < n ; i++ ) { for ( int j = 0 ; j < n ; j++ ) { temp[i][j]=mat[n-1-j][i]; } } return temp; }