Skip to content

Commit 38ad656

Browse files
committed
update offer
1 parent 59d59fb commit 38ad656

File tree

2 files changed

+159
-1
lines changed

2 files changed

+159
-1
lines changed

剑指offer/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212

1313
- [剑指 Offer 03. 数组中重复的数字](./offer03.ipynb) 🌟
1414
- [剑指 Offer 04. 二维数组中的查找](./offer04.ipynb) 🌟🌟
15-
- [剑指 Offer 58 - II. 左旋转字符串](./offer58.ipynb) 🌟
15+
- [剑指 Offer 58 - II. 左旋转字符串](./offer58.ipynb) 🌟
16+
- [剑指 Offer 29. 顺时针打印矩阵](./offer29.ipynb) 🌟

剑指offer/offer29.ipynb

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
{
2+
"metadata": {
3+
"language_info": {
4+
"codemirror_mode": {
5+
"name": "ipython",
6+
"version": 3
7+
},
8+
"file_extension": ".py",
9+
"mimetype": "text/x-python",
10+
"name": "python",
11+
"nbconvert_exporter": "python",
12+
"pygments_lexer": "ipython3",
13+
"version": "3.8.5-final"
14+
},
15+
"orig_nbformat": 2,
16+
"kernelspec": {
17+
"name": "python3",
18+
"display_name": "Python 3.8.5 64-bit",
19+
"metadata": {
20+
"interpreter": {
21+
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
22+
}
23+
}
24+
}
25+
},
26+
"nbformat": 4,
27+
"nbformat_minor": 2,
28+
"cells": [
29+
{
30+
"source": [
31+
"给你一个 `m` 行 `n` 列的矩阵 `matrix` ,请按照 **顺时针螺旋顺序** ,返回矩阵中的所有元素。\n",
32+
"\n",
33+
"**示例 1:** \n",
34+
"<img src=\"https://cdn.jsdelivr.net/gh/MatNoble/Images/20210327125439.png\"/> \n",
35+
"输入:`matrix = [[1,2,3],[4,5,6],[7,8,9]]` \n",
36+
"输出:`[1,2,3,6,9,8,7,4,5]`\n",
37+
"\n",
38+
"**示例 2:** \n",
39+
"<img src=\"https://cdn.jsdelivr.net/gh/MatNoble/Images/20210327125458.png\"/> \n",
40+
"输入:`matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]` \n",
41+
"输出:`[1,2,3,4,8,12,11,10,9,5,6,7]`\n",
42+
"\n",
43+
"**限制:** \n",
44+
"- $0 \\leq matrix.length \\leq 100$\n",
45+
"- $0 \\leq matrix[i].length \\leq 100$\n",
46+
"\n",
47+
"来源:力扣(LeetCode) \n",
48+
"链接:https://leetcode-cn.com/problems/spiral-matrix \n",
49+
"著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。"
50+
],
51+
"cell_type": "markdown",
52+
"metadata": {}
53+
},
54+
{
55+
"source": [
56+
"### **层**遍历\n",
57+
"每一层都按如下方式遍历 \n",
58+
"<img src=\"https://cdn.jsdelivr.net/gh/MatNoble/Images/20210327130635.png\" width=600/>"
59+
],
60+
"cell_type": "markdown",
61+
"metadata": {}
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 1,
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"from typing import List \n",
70+
"class Solution:\n",
71+
" def __init__(self):\n",
72+
" self.res = []\n",
73+
"\n",
74+
" def level(self, matrix, m, n, k):\n",
75+
" \"\"\"k: 记录层数\"\"\"\n",
76+
" # 上\n",
77+
" self.res += [matrix[k][j+k] for j in range(m-1)]\n",
78+
" # 右\n",
79+
" self.res += [matrix[i+k][m-1+k] for i in range(n-1)]\n",
80+
" # 下\n",
81+
" self.res += [matrix[n-1+k][j-k] for j in range(-1, -m ,-1)]\n",
82+
" # 右\n",
83+
" self.res += [matrix[i-k][k] for i in range(-1, -n, -1)]\n",
84+
"\n",
85+
" def spiralOrder(self, matrix: List[List[int]]) -> List[int]:\n",
86+
" # 行列\n",
87+
" if len(matrix) < 1: return []\n",
88+
" n, m = len(matrix), len(matrix[0])\n",
89+
" # k: 层数\n",
90+
" k = 0\n",
91+
" while m > 1 and n > 1:\n",
92+
" self.level(matrix, m, n, k)\n",
93+
" # 更新索引\n",
94+
" k += 1\n",
95+
" m -= 2\n",
96+
" n -= 2\n",
97+
" if n == 1: # 剩余一列\n",
98+
" self.res += [matrix[k][j+k] for j in range(m)]\n",
99+
" elif m == 1: # 剩余一行\n",
100+
" self.res += [matrix[i+k][m-1+k] for i in range(n)]\n",
101+
" return self.res"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": 3,
107+
"metadata": {},
108+
"outputs": [],
109+
"source": [
110+
"mat = Solution()"
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": 4,
116+
"metadata": {},
117+
"outputs": [
118+
{
119+
"output_type": "execute_result",
120+
"data": {
121+
"text/plain": [
122+
"[1, 2, 3, 6, 9, 8, 7, 4, 5]"
123+
]
124+
},
125+
"metadata": {},
126+
"execution_count": 4
127+
}
128+
],
129+
"source": [
130+
"# 测试\n",
131+
"matrix = [[1,2,3],[4,5,6],[7,8,9]]\n",
132+
"mat.spiralOrder(matrix)"
133+
]
134+
},
135+
{
136+
"cell_type": "code",
137+
"execution_count": 5,
138+
"metadata": {},
139+
"outputs": [
140+
{
141+
"output_type": "execute_result",
142+
"data": {
143+
"text/plain": [
144+
"[1, 2, 3, 6, 9, 8, 7, 4, 5, 1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]"
145+
]
146+
},
147+
"metadata": {},
148+
"execution_count": 5
149+
}
150+
],
151+
"source": [
152+
"matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]\n",
153+
"mat.spiralOrder(matrix)"
154+
]
155+
}
156+
]
157+
}

0 commit comments

Comments
 (0)