Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1d961bd

Browse files
committed
update offer
1 parent e00b5c6 commit 1d961bd

File tree

4 files changed

+144
-81
lines changed

4 files changed

+144
-81
lines changed

‎剑指offer/03.py‎

Lines changed: 0 additions & 41 deletions
This file was deleted.

‎剑指offer/04.py‎

Lines changed: 0 additions & 39 deletions
This file was deleted.

‎剑指offer/README.md‎

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

1111
<img src="https://cdn.jsdelivr.net/gh/MatNoble/Images/20210327102030.png" title="数组、字符串" width=600/>
1212

13-
- [剑指 Offer 03. 数组中重复的数字](./offer03.ipynb)
13+
- [剑指 Offer 03. 数组中重复的数字](./offer03.ipynb) 🌟
14+
- [剑指 Offer 04. 二维数组中的查找](./offer04.ipynb) 🌟🌟

‎剑指offer/offer04.ipynb‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
"剑指 Offer 04. 二维数组中的查找\n",
32+
"> **美团面试题**\n",
33+
"\n",
34+
"在一个 $n * m$ 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。\n",
35+
"\n",
36+
"示例: \n",
37+
"现有矩阵 `matrix` 如下:\n",
38+
"```\n",
39+
"[\n",
40+
" [1, 4, 7, 11, 15],\n",
41+
" [2, 5, 8, 12, 19],\n",
42+
" [3, 6, 9, 16, 22],\n",
43+
" [10, 13, 14, 17, 24],\n",
44+
" [18, 21, 23, 26, 30]\n",
45+
"]\n",
46+
"```\n",
47+
"- 给定 `target = 5`,返回 `true`\n",
48+
"- 给定 `target = 20`,返回 `false`\n",
49+
"\n",
50+
"**限制:** \n",
51+
"- 0ドル \\leq n \\leq 1000$\n",
52+
"- 0ドル \\leq m \\leq 1000$\n",
53+
"\n",
54+
"来源:力扣(LeetCode) \n",
55+
"链接:https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof \n",
56+
"著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。"
57+
],
58+
"cell_type": "markdown",
59+
"metadata": {}
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 2,
64+
"metadata": {},
65+
"outputs": [],
66+
"source": [
67+
"from typing import List\n",
68+
"class Solution:\n",
69+
" def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:\n",
70+
" m = len(matrix) # 行数\n",
71+
" if m == 0: return False\n",
72+
" n = len(matrix[0]) # 列数\n",
73+
" i, j = m-1, 0 # 从左下角开始二分查找\n",
74+
" while i >= 0 and j < n:\n",
75+
" if matrix[i][j] < target:\n",
76+
" j += 1\n",
77+
" elif matrix[i][j] > target:\n",
78+
" i -= 1\n",
79+
" else:\n",
80+
" return True\n",
81+
" return False"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 3,
87+
"metadata": {},
88+
"outputs": [],
89+
"source": [
90+
"# 测试\n",
91+
"matrix = [\n",
92+
" [1, 4, 7, 11, 15],\n",
93+
" [2, 5, 8, 12, 19],\n",
94+
" [3, 6, 9, 16, 22],\n",
95+
" [10, 13, 14, 17, 24],\n",
96+
" [18, 21, 23, 26, 30]\n",
97+
"]"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 4,
103+
"metadata": {},
104+
"outputs": [
105+
{
106+
"output_type": "execute_result",
107+
"data": {
108+
"text/plain": [
109+
"True"
110+
]
111+
},
112+
"metadata": {},
113+
"execution_count": 4
114+
}
115+
],
116+
"source": [
117+
"mat = Solution()\n",
118+
"mat.findNumberIn2DArray(matrix, target=5)"
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": 5,
124+
"metadata": {},
125+
"outputs": [
126+
{
127+
"output_type": "execute_result",
128+
"data": {
129+
"text/plain": [
130+
"False"
131+
]
132+
},
133+
"metadata": {},
134+
"execution_count": 5
135+
}
136+
],
137+
"source": [
138+
"mat.findNumberIn2DArray(matrix, target=20)"
139+
]
140+
}
141+
]
142+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /