The matrix clockwise spiral output 2 different methods.

Source: Internet
Author: User

The first method of solution:

Package com.zhm;/** * created by zhm on 2015/6/27. * 1 2  3 * 4 5 6 * 7 8 9 *  the above matrix follows clockwise output: 1,2,3,6,9,8,7,4,5  *  recursive + start and end judgment implementation.  */public class juzhenprint {    /**     *   Initializing array matrix      *  @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&LT;10) {                     tmp =  (int) (Math.random () *100);                 }                 result[j][i]=tmp;                 system.out.print (result[j][i] +  "   " );            }             system.out.println (" ");         }        return result;    }     /**     *  Clockwise output matrix      *  @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);     }}

Results:

61 11 40 26 47 22 76 44 24 61,11,40,22,24,44,76,26,47,

The second method of solution:

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 *  The matrix above is:1,2,3,6,9,8,7,4,5 *  by the rotation matrix + segmentation matrix According to the clockwise output  */public class  JuzhenRotation {    /**     *  Initialize array matrix       *  @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   record number of rotations when count= 4 indicates that a circle has been rotated.      */    private void rotationary (Int[][] juzhen , List<iNteger> result,int count)  {        //recursive to matrix no element exits. Even row * column Matrix         if (juzhen.length==0) {             return;        }         for (int i=0;i<juzhen[0].length-1;i++) {             result.add (juzhen[i][0]);         }        //recursion to the matrix with only one line left, that is, a single element left          if (juzhen.length==1)         {             result.add (juzhen[0][0]);             return;        }         ++count;        //rotation matrix          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) {             //a lap traversal is complete. Remove the ring array             count=0;             //Segmentation Matrix              temp = patitionary (temp);        }         //recursively calls         rotationary (temp, Result,count);     }    private int[][] patitionary (int[][] &NBSP;TEMP)  {        //Remove the outermost circle of the two-dimensional array          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);     }}

Results:

44 74 72 20 88 35 21 25 73 44,74,72,35,73,25,21,20,88,

Two methods should be the first kind of high efficiency, the second one feels good to understand some, completely follow the meaning of the topic solution.

The matrix clockwise spiral output 2 different methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.