Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example,
Given the following matrix:

[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]

You should return [1,2,3,6,9,8,7,4,5].

复用spiral Matrix 代码即可。 注意这里m 不见得等于n, 所以有三种情况。

 1 public class Solution {
 2     public ArrayList<Integer> spiralOrder(int[][] matrix) {
 3         // Note: The Solution object is instantiated only once and is reused by each test case.
 4         if(matrix == null) return null;
 5         ArrayList<Integer> l = new ArrayList<Integer>();
 6         if(matrix.length == 0 || matrix[0].length == 0) return l;
 7         int m = matrix.length;
 8         int n = matrix[0].length;
 9         int top = 0;
10         int bot = m - 1;
11         int left = 0;
12         int right = n - 1;
13         while(top < bot && left <right)
14         {
15              for(int i = left; i < right; i ++)
16              {
17                  l.add(matrix[top][i]);
18              }
19              for(int i = top; i < bot; i ++)
20              {
21                  l.add(matrix[i][right]);
22              }
23              for(int i = right; i > left; i --)
24              {
25                  l.add(matrix[bot][i]);
26              }
27              for(int i = bot; i > top; i --)
28              {
29                  l.add(matrix[i][left]);    
30              }
31              top ++;
32              bot --;
33              left ++;
34              right --;
35         }
36         if(top == bot && left == right)
37         {
38             l.add(matrix[top][left]);
39         }else if(top < bot && left == right)
40         {
41             for(int i = top; i <= bot; i ++)
42             {
43                 l.add(matrix[i][left]);
44             }
45         }else if(left < right && top == bot)
46         {
47             for(int i = left; i <= right; i ++)
48              {
49                  l.add(matrix[top][i]);
50              }
51         }
52         return l;
53     }
54 }

注意三种情况:

1. 这时循环是<=, 因为要一行输出所以 全部都输出。

2. 可能top > bot(left > right), 这说明是偶数行(列), 那么就不用在输出什么了。

 

 

第三遍:

 1 public class Solution {
 2     public List<Integer> spiralOrder(int[][] matrix) {
 3         ArrayList<Integer> result = new ArrayList<Integer>();
 4         if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return result;
 5         int top = 0, bot = matrix.length - 1, left = 0, right = matrix[0].length - 1;
 6         while(top < bot && left < right){
 7             for(int i = left; i < right; i ++)
 8                 result.add(matrix[top][i]);
 9             for(int i = top; i < bot; i ++)
10                 result.add(matrix[i][right]);
11             for(int i = right; i > left; i --)
12                 result.add(matrix[bot][i]);
13             for(int i = bot; i > top; i --)
14                 result.add(matrix[i][left]);
15             top ++; left ++; bot --; right --;
16         }
17         if(top < bot && left == right){
18             for(int i = top; i <= bot; i ++){
19                 result.add(matrix[i][left]);
20             }
21         } else if (top == bot && left < right){
22             for(int i = left; i <= right; i ++){
23                 result.add(matrix[top][i]);
24             }
25         }else if (top == bot && left == right)
26             result.add(matrix[top][left]);
27         return result;
28     }
29 }

 

posted on 2013-10-04 12:07  Step-BY-Step  阅读(148)  评论(0编辑  收藏  举报

导航