标签:
第一种解法:
package com.zhm; /** * Created by zhm on 2015/6/27. * 1 2 3 * 4 5 6 * 7 8 9 * 上面的矩阵按照顺时针输出:1,2,3,6,9,8,7,4,5 * 递归+起止判断实现。 */ public class JuzhenPrint { /** * 初始化数组矩阵 * @param length * @return */ public int[][] initJuzheng(int length){ int[][] result = new int[length][length]; for(int i=0;i<length;i++){ for(int j=0;j<length;j++){ int tmp = (int)(Math.random()*100); while(tmp<10){ tmp = (int)(Math.random()*100); } result[j][i]=tmp; System.out.print(result[j][i] + " "); } System.out.println(" "); } return result; } /** * 顺时针输出矩阵 * @param num * @param start * @param end */ public void output(int[][] num,int start,int end){ if(start>end || end<=0)return; for(int i=start;i<=end;i++){ System.out.print(num[i][start] + ","); } for(int i=start+1;i<=end;i++){ System.out.print(num[end][i] +","); } for(int i=end-1;i>=start;i--){ System.out.print(num[i][end]+","); } for(int i=end-1;i>start;i--){ System.out.print(num[start][i]+","); } output(num,start+1,end-1); } public static void main(String[] args) { JuzhenPrint jz = new JuzhenPrint(); int[][] juzhen = jz.initJuzheng(3); jz.output(juzhen, 0, juzhen.length - 1); } }
结果:
61 11 40 26 47 22 76 44 24 61,11,40,22,24,44,76,26,47,
第二种解法:
package com.zhm; import java.util.ArrayList; import java.util.List; /** * Created by zhm on 2015/6/27. * 1 2 3 * 4 5 6 * 7 8 9 * 上面的矩阵按照顺时针输出:1,2,3,6,9,8,7,4,5 * 通过旋转矩阵+切分矩阵实现 */ public class JuzhenRotation { /** * 初始化数组矩阵 * @param length * @return */ public int[][] initJuzheng(int length){ int[][] result = new int[length][length]; for(int i=0;i<length;i++){ for(int j=0;j<length;j++){ int tmp = (int)(Math.random()*100); while(tmp<10){ tmp = (int)(Math.random()*100); } result[j][i]=tmp; System.out.print(result[j][i] + " "); } System.out.println(" "); } return result; } private void printResult(int[][] juzhen) { List<Integer> result = new ArrayList<Integer>(); rotationAry(juzhen, result, 0); for (Integer data : result) { System.out.print(data + ","); } } /** * * @param juzhen * @param result * @param count 记录旋转次数,当count=4表示已经旋转了一圈。 */ private void rotationAry(int[][] juzhen,List<Integer> result,int count) { //递归至矩阵无元素退出。偶数行*列矩阵 if(juzhen.length==0){ return; } for(int i=0;i<juzhen[0].length-1;i++){ result.add(juzhen[i][0]); } //递归至矩阵只剩一行,也就是只剩一个元素 if(juzhen.length==1) { result.add(juzhen[0][0]); return; } ++count; //旋转矩阵 int[][] temp = new int[juzhen.length][juzhen.length]; for (int i = 0; i <juzhen.length; i++) { for (int j = 0; j <juzhen[i].length; j++) { temp[i][j]=juzhen[juzhen[i].length-j-1][i]; } } if(count==4){ //一圈遍历完成。移除该圈数组 count=0; //切分矩阵 temp = patitionAry(temp); } //递归调用 rotationAry(temp,result,count); } private int[][] patitionAry(int[][] temp) { //把二维数组最外围的一圈去掉 int[][] result = new int[temp.length-2][temp.length-2]; for(int i=0;i<result.length;i++){ for(int j=0;j<result.length;j++){ result[j][i]=temp[j+1][i+1]; } } return result; } public static void main(String[] args) { JuzhenRotation jz = new JuzhenRotation(); int[][] juzhen = jz.initJuzheng(3); jz.printResult(juzhen); } }
结果:
44 74 72 20 88 35 21 25 73 44,74,72,35,73,25,21,20,88,
两种方法应该是第一种效率高,第二种给人感觉好理解一些,完全顺着题目的意思解答的。
标签:
原文地址:http://my.oschina.net/zhmlvft/blog/471456