实现矩阵的转置较为容易,只需要将纵横下标互换即可。实现矩阵旋转稍微麻烦一点。
解题思路:
矩阵转换90度,则原矩阵的纵下标转变为新矩阵的横下标;原矩阵的横下标转变为新矩阵的纵下标,并且顺序相反。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class rotation { public static int [][] change( int [][]matrix){ int [][]temp= new int [matrix[ 0 ].length][matrix.length]; int dst=matrix.length- 1 ; for ( int i= 0 ;i<matrix.length;i++,dst--){ for ( int j= 0 ;j<matrix[ 0 ].length;j++){ temp[j][dst]=matrix[i][j]; } } return temp; }
public static void main(string[]args){ int [][]matrix={{ 1 , 2 , 3 , 4 },{ 5 , 6 , 7 , 8 },{ 9 , 10 , 11 , 12 }}; int [][]temp=change(matrix); for ( int i= 0 ;i<temp.length;i++){ for ( int j= 0 ;j<temp[ 0 ].length;j++){ system.out.print(temp[i][j]+ "\t" ); } system.out.println(); } }
} |
结果如下:
1 2 3 4 |
9 5 1 10 6 2 11 7 3 12 8 4 |
其实并不复杂,然而我在规定时间没有编写出来。。。果然还是需要多练习。
以上这篇java实现矩阵顺时针旋转90度的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
原文链接:https://blog.csdn.net/whuzhang16/article/details/77926341
查看更多关于Java实现矩阵顺时针旋转90度的示例的详细内容...