---
title: "矩阵旋转"
author: "ouqi"
date: "2013-10-22T19:59:37Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2013-10-22T19:59:37Z"
id: 1962458
url: "https://ouqi.iteye.com/blog/1962458"
---

没什么高端算法..注意下标，思维清晰就可以了。

 

```
//矩阵顺时针旋转90度，如果是in-place,则矩阵只能为方阵
//如果可以利用其它存储空间，则矩阵可以是m*n的，转换之后为n*m的
//i'(转换后行) = j（原列）; j'（转换后列）= m-1-i(原行);
//我们这里写就地逆置的
public class ClockWiseTransferMatrixInplace {
	public void transfer(int a[][]){
		if(a == null||a[0]== null) return ;
		if(a.length!= a[0].length) return ;
		int n = a.length;
		
		transfer(a,0,n-1,n);
	}
	
	public void transfer(int a[][],int start,int end,int n){
		if(start>=end) return;
		for(int i = start;i<=end-1;i++){
			int tempa = a[i][start];
			int ti = start;
			int tj = n-1-i;
			while(ti!=i||tj!=start){
				int tempb = a[ti][tj];
				a[ti][tj] = tempa;
				tempa = tempb;
				int k = ti;
				ti = tj;
				tj = n-1-k;
			}
			a[i][start] = tempa;//不要忘记了！
		}
	    transfer(a,start+1,end-1,n);
	}
	
```