Skip to content

Commit 26861b5

Browse files
committed
add 213
1 parent 914395f commit 26861b5

File tree

173 files changed

+23855
-58
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+23855
-58
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ My personal leetcode answers<br/>
66
This is a **continually updated** open source project<br/>
77
<br/>
88
Total sovled: **343**<br/>
9-
Auto updated at: **2024-07-17 19:22:36**<br/>
9+
Auto updated at: **2024-07-19 10:58:47**<br/>
1010

1111
## 软件/Softwares
1212
- [Anki](https://apps.ankiweb.net/)
@@ -195,7 +195,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
195195
| [Leetcode-3048. Earliest Second To Mark Indices I](https://leetcode.com/problems/earliest-second-to-mark-indices-i/description/) | [python](./leetcode/3048.earliest-second-to-mark-indices-i.py), [c++](./leetcode/3048.earliest-second-to-mark-indices-i.cpp) | O\(M\*logM\) | O\(M\) | \- | - |
196196

197197
## Dynamic Programming
198-
| Problem(29) | Solution | Time | Space | Note | Ref |
198+
| Problem(30) | Solution | Time | Space | Note | Ref |
199199
| ----- | ----- | ----- | ----- | ----- | ----- |
200200
| [Leetcode-10. Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/description/) | [python](./leetcode/10.regular-expression-matching.py), [c++](./leetcode/10.regular-expression-matching.cpp) | O\(MN\) | O\(MN\) | \- | - |
201201
| [Leetcode-64. Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/description/) | [c++](./leetcode/64.minimum-path-sum.cpp), [python](./leetcode/64.minimum-path-sum.py) | O\(MN\) | O\(MN\) | \- | - |
@@ -208,6 +208,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
208208
| [Leetcode-139. Word Break](https://leetcode.com/problems/word-break/description/) | [c++](./leetcode/139.word-break.cpp), [python](./leetcode/139.word-break.py) | O\(MN\) | O\(N\) | \- | - |
209209
| [Leetcode-188. Best Time To Buy And Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/) | [python](./leetcode/188.best-time-to-buy-and-sell-stock-iv.py), [c++](./leetcode/188.best-time-to-buy-and-sell-stock-iv.cpp) | O\(NK\) | O\(NK\) | \- | - |
210210
| [Leetcode-198. House Robber](https://leetcode.com/problems/house-robber/description/) | [c++](./leetcode/198.house-robber.cpp), [python](./leetcode/198.house-robber.py) | O\(N\) | O\(N\) | \- | - |
211+
| [Leetcode-213. House Robber II](https://leetcode.com/problems/house-robber-ii/description/) | [c++](./leetcode/213.house-robber-ii.cpp), [python](./leetcode/213.house-robber-ii.py) | O\(N\) | O\(N\) | \- | - |
211212
| [Leetcode-221. Maximal Square](https://leetcode.com/problems/maximal-square/description/) | [python](./leetcode/221.maximal-square.py), [c++](./leetcode/221.maximal-square.cpp) | O\(MN\) | O\(MN\) | \- | - |
212213
| [Leetcode-279. Perfect Squares](https://leetcode.com/problems/perfect-squares/description/) | [c++](./leetcode/279.perfect-squares.cpp), [python](./leetcode/279.perfect-squares.py) | O\(n^\{3/2\}\) | O\(N\) | \- | - |
213214
| [Leetcode-300. Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/description/) | [python](./leetcode/300.longest-increasing-subsequence.py), [c++](./leetcode/300.longest-increasing-subsequence.cpp) | O\(N\*logN\) | O\(N\) | LIS \| std::lower\_bound | - |
@@ -330,7 +331,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
330331
| [Lintcode-1790. Rotate String II](https://www.lintcode.com/problem/rotate-string-ii) | [c++](./lintcode/1790.rotate-string-ii.cpp), [python](./lintcode/1790.rotate-string-ii.py) | O\(N\) | O\(N\) | Simulation | - |
331332

332333
## Other
333-
| Problem(237) | Solution | Time | Space | Note | Ref |
334+
| Problem(236) | Solution | Time | Space | Note | Ref |
334335
| ----- | ----- | ----- | ----- | ----- | ----- |
335336
| [Leetcode-1. Two Sum](https://leetcode.com/problems/two-sum/description/) | [python](./leetcode/1.two-sum.py), [c++](./leetcode/1.two-sum.cpp) | \- | \- | \- | - |
336337
| [Leetcode-2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/) | [python](./leetcode/2.add-two-numbers.py), [c++](./leetcode/2.add-two-numbers.cpp) | \- | \- | \- | - |
@@ -429,7 +430,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
429430
| [Leetcode-210. Course Schedule II](https://leetcode.com/problems/course-schedule-ii/description/) | [c++](./leetcode/210.course-schedule-ii.cpp), [python](./leetcode/210.course-schedule-ii.py) | \- | \- | \- | - |
430431
| [Leetcode-211. Add And Search Word Data Structure Design](https://leetcode.com/problems/add-and-search-word-data-structure-design/description/) | [c++](./leetcode/211.add-and-search-word-data-structure-design.cpp), [python](./leetcode/211.add-and-search-word-data-structure-design.py) | \- | \- | \- | - |
431432
| [Leetcode-212. Word Search II](https://leetcode.com/problems/word-search-ii/description/) | [c++](./leetcode/212.word-search-ii.cpp), [python](./leetcode/212.word-search-ii.py) | \- | \- | \- | - |
432-
| [Leetcode-213. House Robber II](https://leetcode.com/problems/house-robber-ii/description/) | [c++](./leetcode/213.house-robber-ii.cpp), [python](./leetcode/213.house-robber-ii.py) | \- | \- | \- | - |
433433
| [Leetcode-217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/) | [c++](./leetcode/217.contains-duplicate.cpp) | \- | \- | \- | - |
434434
| [Leetcode-219. Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/description/) | [c++](./leetcode/219.contains-duplicate-ii.cpp) | \- | \- | \- | - |
435435
| [Leetcode-228. Summary Ranges](https://leetcode.com/problems/summary-ranges/description/) | [python](./leetcode/228.summary-ranges.py) | \- | \- | \- | - |

blog/leetcode-1.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
layout: post
3+
index: 1
4+
title: "LeetCode-1.两数之和(Two Sum)"
5+
categories: Leetcode
6+
tags: Leetcode Array HashTable
7+
excerpt: "起航,Great Voyage"
8+
---
9+
10+
* content
11+
{:toc}
12+
13+
相信每个想刷leetcode的人,第一次打开网站,就从这道题开始的。
14+
15+
一起起航吧,Great Voyage.
16+
17+
## 1. 两数之和:
18+
19+
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
20+
21+
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
22+
23+
例如:
24+
```
25+
给定 nums = [2, 7, 11, 15], target = 9
26+
27+
因为 nums[0] + nums[1] = 2 + 7 = 9
28+
所以返回 [0, 1]
29+
```
30+
31+
来源:力扣(LeetCode)
32+
33+
链接:[https://leetcode-cn.com/problems/two-sum/](https://leetcode-cn.com/problems/two-sum/)
34+
35+
Link:[https://leetcode.com/problems/two-sum/](https://leetcode.com/problems/two-sum/)
36+
37+
### 哈希
38+
39+
O(N)
40+
41+
遍历数组
42+
43+
如果在字典找到数字,则输出[字典下标,当前下标]
44+
45+
找不到答案,把当前的值放入字典(val -> index)
46+
47+
因为题目说**有且只有一组答案**,所以不用判断数组为空,返回值不存在的情况
48+
49+
```python
50+
class Solution:
51+
def twoSum(self, nums: List[int], target: int) -> List[int]:
52+
map = dict()
53+
for i in range(len(nums)):
54+
val = target - nums[i]
55+
if val in map:
56+
return [map[val], i]
57+
else:
58+
map[nums[i]] = i
59+
```
60+
61+
### 双指针
62+
63+
O(N*logN)
64+
65+
如果题目要求返回两个具体数字,可以先排序,然后使用双指针(start, end)
66+
67+
```res = nums[start] + nums[end]```
68+
69+
如果**res < target**, 则说明**nums[start] + 最大的数**都小于target, start的值太小了,应该start++
70+
71+
如果**res > target**, 则说明**nums[end] + 最小的数**都大于target, end的值太大了,应该end--
72+
73+
如果**res = target**, 则输出答案
74+
75+
```python
76+
class Solution(object):
77+
def twoSum(self, nums: List[int], target: int) -> List[int]:
78+
start = 0
79+
end = len(nums) - 1
80+
81+
nums.sort()
82+
83+
while(start < end):
84+
res = nums[start] + nums[end]
85+
if res == target:
86+
return (nums[start], nums[end])
87+
elif res < target:
88+
start += 1
89+
else:
90+
end -= 1
91+
92+
```
93+
94+
--End--
95+
96+

blog/leetcode-10.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
---
2+
layout: post
3+
index: 10
4+
title: "LeetCode-10.正则表达式匹配(Regular Expression Matching)"
5+
categories: Leetcode
6+
tags: Leetcode Math DP Backtracking
7+
excerpt: "心态爆炸之旅"
8+
---
9+
10+
* content
11+
{:toc}
12+
13+
⚠️前方,高能预警
14+
15+
这是第二道Hard的题目, 属于劝退类型
16+
17+
## 10. 正则表达式匹配
18+
19+
给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配。
20+
21+
'.' 匹配任意单个字符
22+
23+
'*' 匹配零个或多个前面的那一个元素
24+
25+
所谓匹配,是要涵盖整个字符串s的,而不是部分字符串。
26+
27+
说明:
28+
29+
s 可能为空,且只包含从 a-z 的小写字母。
30+
31+
p 可能为空,且只包含从 a-z 的小写字母,以及字符 . 和 *
32+
33+
示例 1:
34+
35+
```
36+
输入:
37+
s = "aa"
38+
p = "a"
39+
输出: false
40+
解释: "a" 无法匹配 "aa" 整个字符串。
41+
```
42+
43+
示例 2:
44+
45+
```
46+
输入:
47+
s = "aa"
48+
p = "a*"
49+
输出: true
50+
解释: 因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。
51+
```
52+
53+
示例 3:
54+
55+
```
56+
输入:
57+
s = "ab"
58+
p = ".*"
59+
输出: true
60+
解释: ".*" 表示可匹配零个或多个('*')任意字符('.')。
61+
```
62+
63+
示例 4:
64+
65+
```
66+
输入:
67+
s = "aab"
68+
p = "c*a*b"
69+
输出: true
70+
解释: 因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"。
71+
```
72+
73+
示例 5:
74+
75+
```
76+
输入:
77+
s = "mississippi"
78+
p = "mis*is*p*."
79+
输出: false
80+
```
81+
82+
来源:力扣(LeetCode)
83+
84+
链接:[https://leetcode-cn.com/problems/regular-expression-matching/](https://leetcode-cn.com/problems/regular-expression-matching/)
85+
86+
Link:[https://leetcode.com/problems/regular-expression-matching/](https://leetcode.com/problems/regular-expression-matching/)
87+
88+
### 暴力破解
89+
90+
将字符串p中的字符*全排列展开,直到p与s长度相等为止,判断s是否和p一致
91+
92+
判断过程中,由于'.'可以匹配任意字符串,可以认为相等
93+
94+
### 动态规划
95+
96+
O(N^2)
97+
98+
定义xxx代表前i个字符串已经能够匹配,这里需要两个变量(i, j)分别代表字符串s和p
99+
100+
table[i][j] = True, 代表s前i个字符能够和p前j个字符匹配
101+
102+
#### 思考步骤
103+
104+
动态规划,先从最后一步倒推,假设字符串s和p能够匹配,那么最后一步可能是
105+
106+
> 下图中认为xxxxxxx部分已完成判断
107+
108+
条件1, 最后一步, s和p最后两个字母相等**或者**p最后一个字母是'.'
109+
110+
```python
111+
xxxxxxxxxxxx a
112+
xxxxxxxxxxxx a
113+
114+
xxxxxxxxxxxx a
115+
xxxxxxxxxxxx .
116+
117+
table[i][j] = ((p[i - i] == s[i - i] or p[i - i] == '.') and table[i - 1][j - 1])
118+
```
119+
120+
条件2, 最后一步, p最后一个是\*, 无论是否能够匹配, 字符\*都可以零次展开
121+
122+
```python
123+
xxxxxxxxxxx x a
124+
xxxxxxxxxxx a *
125+
^
126+
127+
xxxxxxxxxxx x a
128+
xxxxxxxxxxx c *
129+
^
130+
# 0次展开,消耗p字符串*前面的一个字母, 用^来标记, 但s保持不变
131+
132+
table[i][j] = table[i][j - 2]
133+
```
134+
135+
条件3, p最后一个是\*, 当\*一次展开时: (0和1次展开, 就可以覆盖0...k次的情况)
136+
137+
```python
138+
xxxxxxxxxxx x a xxxxxxxxxxx x a
139+
^
140+
xxxxxxxxxxx a * xxxxxxxxxxx a a *
141+
142+
->
143+
xxxxxxxxxxx x a xxxxxxxxxxx x a
144+
^
145+
xxxxxxxxxxx . * xxxxxxxxxxx . . *
146+
147+
# 1次展开,消耗掉s字符串^对应的字母,但是p保持不变
148+
149+
table[i][j] = (p[j - 2] == s[i] or p[i - 2] == '.') and table[i - 1][j])
150+
```
151+
152+
所有条件,只要有一个可以匹配,就可以认为匹配
153+
154+
#### 计算方向
155+
156+
从左到右,从比较短的字符开始,这样长字符,能够利用上短字符的计算结果
157+
158+
#### 边界条件
159+
160+
初始化table内容为False, 矩阵行列记得多开一个空间
161+
162+
其中table[0][0] = True, 代表两个空字符串可以匹配
163+
164+
table[i][0] = False, 也就是p为空,那除了s也为空外,都不能匹配
165+
166+
> ⚠️注意,前i个字符,对应的当前下标是i - 1。
167+
168+
```python
169+
170+
class Solution:
171+
def isMatch(self, s: str, p: str) -> bool:
172+
m = len(s)
173+
n = len(p)
174+
table = [[False for _ in range(n + 1)] for _ in range(m + 1)]
175+
176+
for i in range(m + 1):
177+
for j in range(n + 1):
178+
if i == 0 and j == 0:
179+
table[i][j] = True
180+
continue
181+
182+
if j == 0:
183+
table[i][j] = False
184+
continue
185+
186+
if p[j - 1] != '*':
187+
if i - 1 >= 0 and (p[j - 1] == '.' or p[j - 1] == s[i- 1]):
188+
table[i][j] = table[i - 1][j - 1] # 尾字母可匹配
189+
else:
190+
if j - 2 >= 0:
191+
table[i][j] = table[i][j - 2] # 0次展开
192+
193+
if i - 1 >= 0 and (p[j - 2] == '.' or p[j - 2] == s[i - 1]):
194+
table[i][j] = table[i][j] or table[i - 1][j] # 1次展开
195+
196+
return table[m][n]
197+
```
198+
199+
--End--
200+
201+
难度爆炸,随便的草稿,不值得细看
202+
203+
![草稿说明]({{site.static}}/images/leetcode-sketch-algorithm-10.png)

0 commit comments

Comments
 (0)