Spiral Matrix
leetcode網址
Idea
分成四個case,keep四個方向: top
, down
, right
, left
,然後需要兩個位移動的index: i
走x軸,j
走y軸
- 往右走
- 要bound住右邊
- 走完後j=right(要往下走的column), i++(到下一層), right–(右邊bound減一)
- code
- 往下走
- 要bound住下界
- 走完後i=down(要往左走的row), j–(column往回走), down–(下界bound減一)
- code
- 往左走
- 要bound住左邊
- 走完後j=left(要往上走的column), i–(到上一層), left++(左邊bound加一)
- code
- 往上走
- 要bound住上邊
- 走完後i=top(要往又走的row), j++(column往右走), top++(上邊bound加一)
- code
Solution
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Time Complexity: O (m × n)
✶⋆.˚ Space Complexity: O(1) additional space.