题目
英文
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Example 1:
Input:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input:
[
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
中文
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
示例 1:
输入:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]
示例 2:
输入:
[
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12]
]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]
思路
- 所有走过的节点标记为null
- 用turn标记当前走向,如果下一个节点为null或notSet,则调转到下一个方向。
代码
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
 | class Solution {     function spiralOrder($matrix) {         $row = count($matrix);         if($row == 0)         {             return [];         }         $columu = count($matrix[1]);         $currentpoint = $matrix[0][0];         $turn = 0;         $i = 0;         $j = 0;         while(isset($currentpoint) && !is_null($currentpoint))         {                          $ret[] = $currentpoint;             $matrix[$i][$j] = null;             if($turn == 0)             {                                  if(isset($matrix[$i][$j+1])&&!is_null($matrix[$i][$j+1]))                 {                     $currentpoint = $matrix[$i][$j+1];                     $j++;                 } elseif(isset($matrix[$i+1][$j])&&!is_null($matrix[$i+1][$j])) {                     $currentpoint = $matrix[$i+1][$j];                     $i++;                     $turn = 1;                     continue;                 } else {                     $currentpoint = null;                 }             }                          if($turn == 1)             {                 if(isset($matrix[$i+1][$j])&&!is_null($matrix[$i+1][$j]))                 {                     $currentpoint = $matrix[$i+1][$j];                     $i++;                 } elseif(isset($matrix[$i][$j-1])&&!is_null($matrix[$i][$j-1])) {                     $currentpoint = $matrix[$i][$j-1];                     $j--;                     $turn = 2;                     continue;                 } else {                     $currentpoint = null;                 }             }                          if($turn == 2)             {                   if(isset($matrix[$i][$j-1])&&!is_null($matrix[$i][$j-1]))                 {                     $currentpoint = $matrix[$i][$j-1];                     $j--;                 } elseif(isset($matrix[$i-1][$j])&&!is_null($matrix[$i-1][$j])) {;                     $currentpoint = $matrix[$i-1][$j];                     $i--;                     $turn = 3;                     continue;                 } else {                     $currentpoint = null;                 }             }             if($turn == 3)             {                 if(isset($matrix[$i-1][$j])&&!is_null($matrix[$i-1][$j]))                 {                     $currentpoint = $matrix[$i-1][$j];                     $i--;                 } elseif(isset($matrix[$i][$j+1])&&!is_null($matrix[$i][$j+1])) {                     $currentpoint = $matrix[$i][$j+1];                     $j++;                     $turn = 0;                     continue;                 } else {                     $currentpoint = null;                 }             }         }         return $ret;     } }
 |