From 69377ddec1366c81dbe9343147e2c64de7bf3368 Mon Sep 17 00:00:00 2001 From: maolu Date: Mon, 15 Jan 2024 17:48:20 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=96=B0=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=EF=BC=9A=E8=9E=BA=E6=97=8B=E7=9F=A9=E9=98=B5=E5=AE=9A?= =?UTF-8?q?=E4=B9=894=E4=B8=AA=E8=BE=B9=E7=95=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++++ .idea/encodings.xml | 4 ++ .../inspectionProfiles/profiles_settings.xml | 6 +++ .idea/leetcode-master.iml | 8 ++++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ problems/0054.螺旋矩阵.md | 41 +++++++++++++++++ problems/0059.螺旋矩阵II.md | 44 +++++++++++++++++++ 9 files changed, 129 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/leetcode-master.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 00000000..15a15b21 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml new file mode 100644 index 00000000..d0876a78 --- /dev/null +++ b/.idea/leetcode-master.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..4d519243 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..7c250acd --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/problems/0054.螺旋矩阵.md b/problems/0054.螺旋矩阵.md index 44a7749d..85e6a936 100644 --- a/problems/0054.螺旋矩阵.md +++ b/problems/0054.螺旋矩阵.md @@ -306,6 +306,47 @@ class Solution(object): return result ``` +版本二:定义四个边界 +```python +class Solution(object): + def spiralOrder(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: List[int] + """ + if not matrix: + return [] + + rows = len(matrix) + cols = len(matrix[0]) + top, bottom, left, right = 0, rows - 1, 0, cols - 1 + print_list = [] + + while top <= bottom and left <= right: + # 从左到右 + for i in range(left, right + 1): + print_list.append(matrix[top][i]) + top += 1 + + # 从上到下 + for i in range(top, bottom + 1): + print_list.append(matrix[i][right]) + right -= 1 + + # 从右到左 + if top <= bottom: + for i in range(right, left - 1, -1): + print_list.append(matrix[bottom][i]) + bottom -= 1 + + # 从下到上 + if left <= right: + for i in range(bottom, top - 1, -1): + print_list.append(matrix[i][left]) + left += 1 + + return print_list +```

diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index 58378ffc..8823a0d8 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -207,6 +207,50 @@ class Solution: return nums ``` +版本二:定义四个边界 +```python +class Solution(object): + def generateMatrix(self, n): + if n <= 0: + return [] + + # 初始化 n x n 矩阵 + matrix = [[0]*n for _ in range(n)] + + # 初始化边界和起始值 + top, bottom, left, right = 0, n-1, 0, n-1 + num = 1 + + while top <= bottom and left <= right: + # 从左到右填充上边界 + for i in range(left, right + 1): + matrix[top][i] = num + num += 1 + top += 1 + + # 从上到下填充右边界 + for i in range(top, bottom + 1): + matrix[i][right] = num + num += 1 + right -= 1 + + # 从右到左填充下边界 + + for i in range(right, left - 1, -1): + matrix[bottom][i] = num + num += 1 + bottom -= 1 + + # 从下到上填充左边界 + + for i in range(bottom, top - 1, -1): + matrix[i][left] = num + num += 1 + left += 1 + + return matrix +``` + ### JavaScript: ```javascript From 0e34b46a1777988cf1e618d4e227d02b7f1a82ae Mon Sep 17 00:00:00 2001 From: maolu Date: Mon, 15 Jan 2024 18:03:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=A0=E9=99=A4.idea?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 -------- .idea/encodings.xml | 4 ---- .idea/inspectionProfiles/profiles_settings.xml | 6 ------ .idea/leetcode-master.iml | 8 -------- .idea/misc.xml | 4 ---- .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 7 files changed, 44 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml delete mode 100644 .idea/leetcode-master.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b81..00000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index 15a15b21..00000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2da..00000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/leetcode-master.iml b/.idea/leetcode-master.iml deleted file mode 100644 index d0876a78..00000000 --- a/.idea/leetcode-master.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 4d519243..00000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 7c250acd..00000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1ddf..00000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file