矩阵顺时针螺旋输出2种不同的方法。

124440_aXPv_925404.jpg

第一种解法:

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,

两种方法应该是第一种效率高,第二种给人感觉好理解一些,完全顺着题目的意思解答的。

转载于:https://my.oschina.net/zhmlvft/blog/471456

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值