O(1)空间复杂度实现n*n矩阵旋转90度,
#include <iostream> using namespace std; #define ARRAY_SIZE 5 void print_two_array (int a[][ARRAY_SIZE]) { cout << endl; for (int i=0; i<ARRAY_SIZE; i++) { for (int j=0; j<ARRAY_SIZE; j++) { cout << a[i][j] << ","; } cout << endl; } cout << endl; } void in_place_retation (int a[][ARRAY_SIZE]) { int swap = 0; const int n = ARRAY_SIZE; for (int k=0; k<n/2; k++) { for (int i=k; i<n-k-1; i++) { swap = a[k][i]; a[k][i] = a[i][n-k-1]; a[i][n-k-1] = a[n-k-1][n-i-1]; a[n-k-1][n-i-1] = a[n-i-1][k]; a[n-i-1][k] = swap; //print_two_array(a); } //print_two_array(a); } } int main () { int a[ARRAY_SIZE][ARRAY_SIZE] = { 1,2,3,4,5, 6,7,8,9,10, 11,12,13,14,15, 16,17,18,19,20, 21,22,23,24,25 }; print_two_array(a); in_place_retation(a); print_two_array(a); }
本文出自 “天眼神童的新家” 博客,请务必保留此出处http://tianyanshentong.blog.51cto.com/3356396/1559202
原文地址:http://tianyanshentong.blog.51cto.com/3356396/1559202