几个面试算法题

题目一,

由外向内顺时针打印一个方阵的各元素。比如

1  2  3  4

5  6  7  8

9  10 11 12

13  14 15 16

输出1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

package com.harry;

import org.testng.annotations.Test;

public class testClockwiseOutput { //顺时针打印一个矩阵

    public static int[][] produceMatrix(int m,int n){
        int[][] matrix = new int[m][n];
        for(int i=0;i=end || end<=0)return;
        for(int i=start;i<=end;i++){
            System.out.print(num[start][i]);
            System.out.print(",");
        } for(int i=start+1;i<=end;i++){
            System.out.print(num[i][end]);
            System.out.print(",");
        } for(int i=end-1;i>=start;i--){
            System.out.print(num[end][i]);
            System.out.print(",");
        } for(int i=end-1;i>start;i--){
            System.out.print(num[i][start]);
            System.out.print(",");
        }
        output(num,start+1,end-1);
    }
}

待续。 

 

你可能感兴趣的:(Java)