Python、C++、JavaScript、SQL、TypeScript の多様な LeetCode ソリューションを探索してください。面接の準備、学習、複数のプログラミング言語でのコードの練習に最適です。 Github リポジトリ リンク
あなたは独房から始めます (rスタート、cスタート)
の 行×列
東向きのグリッドでは、北西の角がグリッドの最初の行と列にあり、南東の角が最後の行と列にあります。
時計回りの螺旋状に歩き、グリッドの境界の外側に移動するたびに、グリッドの外側を歩き続けます (ただし、後でグリッドの境界に戻ることもあります)。 行数 * 列数
グリッドのスペース。
戻る グリッドの位置を訪問順に表す座標の配列.
目次
トグルパイソン
# 時間計算量: O(n^2) # 空間計算量: import リスト クラスの入力による O(n^2) 解決策: def spiralMatrixIII(self, rows: int,cols: int, rStart: int, cStart: int) -> List[List[int]]: dirMap = [[0, 1], [1, 0], [0, -1], [-1, 0]] トラバース = [] 方向 = 0 ステップ = 1 while len( traverse) < rows *cols: for _ in range(2): for _ in range(step): if (rStart < rows and cStart <cols and rStart >= 0 and cStart >= 0): traverse.append([rStart 、 cStart]) rStart += dirMap[direction][0] cStart += dirMap[direction][1] 方向 = (方向 + 1) % 4 ステップ += 1 戻りトラバース行 = 5 列 = 6 rStart = 1 cStart = 4 print(Solution().spiralMatrixIII(rows,cols,rStart,cStart))