|
1 | | -package Solution; |
2 | | - |
3 | | -import java.util.ArrayList; |
4 | | - |
5 | | -public class SpiralMatrix { |
6 | | - |
7 | | - /** |
8 | | - * @param args |
9 | | - */ |
10 | | - public static void main(String[] args) { |
11 | | - // TODO Auto-generated method stub |
12 | | - SpiralMatrix instance=new SpiralMatrix(); |
13 | | - int [][]arr={{6,9,7}}; |
14 | | - ArrayList<Integer> result=instance.spiralOrder(arr); |
15 | | - System.out.println(result); |
16 | | - } |
17 | | - |
18 | | - public ArrayList<Integer> spiralOrder(int[][] matrix) { |
19 | | - // Start typing your Java solution below |
20 | | - // DO NOT write main() function\ |
| 1 | +public class Solution { |
| 2 | + public ArrayList<Integer> spiralOrder(int[][] matrix) { |
| 3 | + // IMPORTANT: Please reset any member data you declared, as |
| 4 | + // the same Solution instance will be reused for each test case. |
21 | 5 | ArrayList<Integer> result=new ArrayList<Integer>(); |
22 | 6 | if (matrix==null || matrix.length<1 || matrix[0]==null || matrix[0].length<1) |
23 | 7 | { |
24 | 8 | return result; |
25 | 9 | } |
26 | | - int m=matrix.length; |
27 | | - int n=matrix[0].length; |
28 | | - int spiral=(m/2)>(n/2)?(n/2):(m/2); |
29 | | - // the main algorithm |
30 | | - // this should work with the case m is even |
31 | | - int i; |
32 | | - for (i=0; i<spiral; i++) |
| 10 | + int round=Math.min(matrix.length, matrix[0].length)/2; |
| 11 | + for (int i=0; i<round; i++) |
33 | 12 | { |
34 | | - // for left-right |
35 | | - for (int j=i; j<n-i-1; j++) |
| 13 | + // left to right |
| 14 | + for (int j=i; j<matrix[i].length-i-1; j++) |
36 | 15 | { |
37 | 16 | result.add(matrix[i][j]); |
38 | 17 | } |
39 | | - // for top-down |
40 | | - for (int j=i; j<m-i-1; j++) |
| 18 | + // top to bottom |
| 19 | + for(int j=i; j<matrix.length-i-1; j++) |
41 | 20 | { |
42 | | - result.add(matrix[j][n-i-1]); |
| 21 | + result.add(matrix[j][matrix[j].length-i-1]); |
43 | 22 | } |
44 | | - // for right-left |
45 | | - for (int j=n-i-1; j>i; j--) |
| 23 | + // right to left |
| 24 | + for (int j=matrix[matrix.length-i-1].length-1-i; j>i; j--) |
46 | 25 | { |
47 | | - result.add(matrix[m-i-1][j]); |
| 26 | + result.add(matrix[matrix.length-i-1][j]); |
48 | 27 | } |
49 | | - // for bottom-top |
50 | | - for (int j=m-i-1; j>i; j--) |
| 28 | + // bottom to top |
| 29 | + for (int j=matrix.length-i-1; j>i; j--) |
51 | 30 | { |
52 | 31 | result.add(matrix[j][i]); |
53 | 32 | } |
54 | 33 | } |
55 | | - // post-processing, check the special case, where m is odd |
56 | | - if (n==1 || m==1) |
| 34 | + // post-processing, we will have some element unprocessed |
| 35 | + // this only happens the row and column is not even and equal |
| 36 | + if (round*2!=matrix.length && round*2!=matrix[0].length) |
57 | 37 | { |
58 | | - for (i=0; i<m; i++) |
| 38 | + // top to bottom |
| 39 | + if (matrix.length<matrix[0].length) |
59 | 40 | { |
60 | | - for (int j=0; j<n; j++) |
| 41 | + for (int j=round; j<matrix[round].length-round; j++) |
61 | 42 | { |
62 | | - result.add(matrix[i][j]); |
| 43 | + result.add(matrix[round][j]); |
63 | 44 | } |
64 | 45 | } |
65 | | - } |
66 | | - else if(n<m && n%2>0) |
67 | | - { |
68 | | - for (int j=i; j<=m-i-1; j++) |
69 | | - { |
70 | | - result.add(matrix[j][i]); |
71 | | - } |
72 | | - } |
73 | | - else if(m<=n && m%2>0) |
74 | | - { |
75 | | - // we have one row left out |
76 | | - for (int j=i; j<=n-i-1; j++) |
| 46 | + else |
77 | 47 | { |
78 | | - result.add(matrix[i][j]); |
| 48 | + for (int j=round; j<matrix.length-round; j++) |
| 49 | + { |
| 50 | + result.add(matrix[j][round]); |
| 51 | + } |
79 | 52 | } |
80 | 53 | } |
81 | 54 | return result; |
|
0 commit comments