螺旋矩陣

鏈接:https://leetcode-cn.com/problems/spiral-matrix

題目:

給你一個(gè) m 行 n 列的矩陣 matrix ,請(qǐng)按照 順時(shí)針螺旋順序 ,返回矩陣中的所有元素。

示例 1:


示例1.png

輸入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
輸出:[1,2,3,6,9,8,7,4,5]

示例 2:


示例2.png

輸入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
輸出:[1,2,3,4,8,12,11,10,9,5,6,7]

提示:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 10
-100 <= matrix[i][j] <= 100

解題思路

方法一:模擬

可以模擬螺旋矩陣的路徑。初始位置是矩陣的左上角,初始方向是向右,當(dāng)路徑超出界限或者進(jìn)入之前訪問過的位置時(shí),順時(shí)針旋轉(zhuǎn),進(jìn)入下一個(gè)方向。

判斷路徑是否進(jìn)入之前訪問過的位置需要使用一個(gè)與輸入矩陣大小相同的輔助矩陣 \textit{visited}visited,其中的每個(gè)元素表示該位置是否被訪問過。當(dāng)一個(gè)元素被訪問時(shí),將 \textit{visited}visited 中的對(duì)應(yīng)位置的元素設(shè)為已訪問。

如何判斷路徑是否結(jié)束?由于矩陣中的每個(gè)元素都被訪問一次,因此路徑的長度即為矩陣中的元素?cái)?shù)量,當(dāng)路徑的長度達(dá)到矩陣中的元素?cái)?shù)量時(shí)即為完整路徑,將該路徑返回。

方法一的Golang代碼實(shí)現(xiàn)

//方法一 模擬
func spiralOrder(matrix [][]int) []int {
    rows, columns := len(matrix), len(matrix[0])
    totals := rows * columns
    visited := make([][]bool, rows)
    for i := 0 ;i< rows;i++ {
        visited[i] = make([]bool, columns)
    }
    directions := [][]int{{0,1}, {1,0}, {0,-1}, {-1, 0}} //這是從左上角開始,順時(shí)針旋轉(zhuǎn)時(shí),下標(biāo)的增量值,所以順序不能倒
    var (
        row, column = 0, 0
        directionindex = 0
        order = make([]int, totals)
    )
    for i := 0;i< totals;i++{
        order[i] = matrix[row][column]
        visited[row][column] = true
        nextrow, nextcolumn := row + directions[directionindex][0], column + directions[directionindex][1]
        //注意此處幾項(xiàng)的順序,visited[nextrow][nextcolumn]必須放在最后,因?yàn)橐扰袛嗤阯extrow,nextcolumn是否合適,才能再判斷visited[nextrow][nextcolumn]否則可能越界或者不存在。
        if nextrow < 0 ||nextrow >= rows || nextcolumn < 0 || nextcolumn >= columns ||  visited[nextrow][nextcolumn]  {
            directionindex = (directionindex + 1 ) % 4
        }
       row += directions[directionindex][0]
       column += directions[directionindex][1]
    }
    return order
}

方法一的復(fù)雜度分析

時(shí)間復(fù)雜度:O(mn)O(mn),其中 mm 和 nn 分別是輸入矩陣的行數(shù)和列數(shù)。矩陣中的每個(gè)元素都要被訪問一次。

空間復(fù)雜度:O(mn)O(mn)。需要?jiǎng)?chuàng)建一個(gè)大小為 m \times nm×n 的矩陣 \textit{visited}visited 記錄每個(gè)位置是否被訪問過。

方法二:按層模擬

可以將矩陣看成若干層,首先輸出最外層的元素,其次輸出次外層的元素,直到輸出最內(nèi)層的元素。

定義矩陣的第 kk 層是到最近邊界距離為 kk 的所有頂點(diǎn)。例如,下圖矩陣最外層元素都是第 11 層,次外層元素都是第 22 層,剩下的元素都是第 33 層。

[[1, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 2, 2, 1],
[1, 2, 3, 3, 3, 2, 1],
[1, 2, 2, 2, 2, 2, 1],
[1, 1, 1, 1, 1, 1, 1]]
對(duì)于每層,從左上方開始以順時(shí)針的順序遍歷所有元素。假設(shè)當(dāng)前層的左上角位于 (top,left),右下角位于 (bottom,right),按照如下順序遍歷當(dāng)前層的元素。

從左到右遍歷上側(cè)元素,依次為 (top,left)(top,right)。

從上到下遍歷右側(cè)元素,依次為 (top+1,right)(bottom,right)
如果 left<righttop<bottom,則從右到左遍歷下側(cè)元素,依次為 (bottom,right?1)(bottom,left+1),以及從下到上遍歷左側(cè)元素,依次為 (bottom,left)(top+1,left)。

遍歷完當(dāng)前層的元素之后,將 lefttop 分別增加 11,將 rightbottom 分別減少 11,進(jìn)入下一層繼續(xù)遍歷,直到遍歷完所有元素為止。

image.png

方法二的Golang代碼實(shí)現(xiàn)

func spiralOrder(matrix [][]int) []int {
   rows, columns := len(matrix), len(matrix[0])
   totals := rows * columns
   var (
       left, right = 0, columns-1
       top, bottom = 0, rows-1
       order = make([]int, totals)
       index = 0
   )
   for left <= right && top <= bottom {
       //從左上到右上
       for column := left ;column <= right;column++{
           order[index] = matrix[top][column]
           index ++
       }
       //從右上到右下
       for row := top + 1; row <= bottom;row++ {
           order[index] = matrix[row][right]
           index++
       }
       if left < right && top < bottom {
           //從右下到左下
           for column := right - 1; column > left;column--{
               order[index] = matrix[bottom][column]
               index++
           }
           //從左下到左上
           for row := bottom; row > top; row--{
               order[index] = matrix[row][left]
               index++
           }
       }
       left++
       right--
       top++
       bottom--
   }
   return order
}

方法二的復(fù)雜度分析

復(fù)雜度分析

時(shí)間復(fù)雜度:O(mn)O(mn),其中 mm 和 nn 分別是輸入矩陣的行數(shù)和列數(shù)。矩陣中的每個(gè)元素都要被訪問一次。

空間復(fù)雜度:O(1)O(1)。除了輸出數(shù)組以外,空間復(fù)雜度是常數(shù)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • LeetCode上螺旋矩陣[https://leetcode-cn.com/problems/spiral-mat...
    Taec0123閱讀 205評(píng)論 0 0
  • 給你一個(gè) m 行 n 列的矩陣 matrix ,請(qǐng)按照 順時(shí)針螺旋順序 ,返回矩陣中的所有元素 https://l...
    Shimmer_閱讀 293評(píng)論 0 1
  • 1.類型 :數(shù)組 2.題目描述 給定一個(gè)m x n大小的矩陣(m行,n列),按螺旋的順序返回矩陣中的所有元素。示例...
    你若安好_4a49閱讀 477評(píng)論 0 0
  • 54. 螺旋矩陣給定一個(gè)包含 m x n 個(gè)元素的矩陣(m 行, n 列),請(qǐng)按照順時(shí)針螺旋順序,返回矩陣中的所有...
    杏仁小核桃閱讀 299評(píng)論 0 2
  • 【leetcode-數(shù)組】螺旋矩陣 II 題目: 給定一個(gè)正整數(shù) n,生成一個(gè)包含 1 到 n2 所有元素,且元素...
    程序員小2閱讀 186評(píng)論 0 1

友情鏈接更多精彩內(nèi)容