fork(2) download
  1. import java.io.*;
  2. import java.util.Arrays;
  3.  
  4. class Ideone
  5. {
  6. static int[] f(int a[][]){int i,d=0,k=0,l=0,n=a[0].length,m=a.length;int[]r=new int[m*n];if(a==null)return r;while(k<m&&l<n){for(i=l;i<n;++i)r[d++]=a[k][i];k++;for(i=k;i<m;++i)r[d++]=a[i][n-1];n--;if(k<m)for(i=n-1;i>=l;--i)r[d++]=a[m-1][i];m--;if(l<n)for(i=m-1;i>=k;--i)r[d++]=a[i][l];l++;}return r;}
  7.  
  8. public static void main (String[] args)
  9. {
  10. int a[][] = {{}};
  11. int b[][] = {{1,2,3,4},{12,13,14,5},{11,16,15,6},{10,9,8,7}};
  12. int c[][] = {{1, 2, 3, 4, 5, 6},{7, 8, 9, 10, 11, 12},{13, 14, 15, 16, 17, 18}};
  13.  
  14. System.out.println(Arrays.toString(f(a)));
  15. System.out.println(Arrays.toString(f(b)));
  16. System.out.println(Arrays.toString(f(c)));
  17. }
  18. }
Success #stdin #stdout 0.07s 2184192KB
stdin
Standard input is empty
stdout
[]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[1, 2, 3, 4, 5, 6, 12, 18, 17, 16, 15, 14, 13, 7, 8, 9, 10, 11]