Skip to content

Commit 3654f7d

Browse files
committed
Update SpiralMatrix.java
Submission Result: Runtime Error Last executed input: [[6,9,7]] Submission Result: Wrong Answer Input: [[2,3]] Output: [] Expected: [2,3] Submission Result: Wrong Answer Input: [[2,3]] Output: [2] Expected: [2,3] Submission Result: Wrong Answer Input: [[1,2,3,4],[5,6,7,8],[9,10,11,12]] Output: [1,2,3,4,8,12,11,10,9,5] Expected: [1,2,3,4,8,12,11,10,9,5,6,7]
1 parent f4b6019 commit 3654f7d

File tree

1 file changed

+28
-55
lines changed

1 file changed

+28
-55
lines changed

src/Solution/SpiralMatrix.java

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,54 @@
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.
215
ArrayList<Integer> result=new ArrayList<Integer>();
226
if (matrix==null || matrix.length<1 || matrix[0]==null || matrix[0].length<1)
237
{
248
return result;
259
}
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++)
3312
{
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++)
3615
{
3716
result.add(matrix[i][j]);
3817
}
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++)
4120
{
42-
result.add(matrix[j][n-i-1]);
21+
result.add(matrix[j][matrix[j].length-i-1]);
4322
}
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--)
4625
{
47-
result.add(matrix[m-i-1][j]);
26+
result.add(matrix[matrix.length-i-1][j]);
4827
}
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--)
5130
{
5231
result.add(matrix[j][i]);
5332
}
5433
}
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)
5737
{
58-
for (i=0; i<m; i++)
38+
// top to bottom
39+
if (matrix.length<matrix[0].length)
5940
{
60-
for (int j=0; j<n; j++)
41+
for (int j=round; j<matrix[round].length-round; j++)
6142
{
62-
result.add(matrix[i][j]);
43+
result.add(matrix[round][j]);
6344
}
6445
}
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
7747
{
78-
result.add(matrix[i][j]);
48+
for (int j=round; j<matrix.length-round; j++)
49+
{
50+
result.add(matrix[j][round]);
51+
}
7952
}
8053
}
8154
return result;

0 commit comments

Comments
 (0)