add #18.2 #19 python implement
This commit is contained in:
parent
dec6f7af69
commit
2ab1a21f34
132
notes/面试总结.md
132
notes/面试总结.md
@ -20,6 +20,8 @@
|
|||||||
* [16. 数值的整数次方](#16-数值的整数次方)
|
* [16. 数值的整数次方](#16-数值的整数次方)
|
||||||
* [17. 打印从 1 到最大的 n 位数](#17-打印从-1-到最大的-n-位数)
|
* [17. 打印从 1 到最大的 n 位数](#17-打印从-1-到最大的-n-位数)
|
||||||
* [18.1 在 O(1) 时间内删除链表节点](#181-在-o1-时间内删除链表节点)
|
* [18.1 在 O(1) 时间内删除链表节点](#181-在-o1-时间内删除链表节点)
|
||||||
|
* [18.2 删除链表中重复的结点](#182-删除链表中重复的结点)
|
||||||
|
* [19. 正则表达式匹配](#19-正则表达式匹配)
|
||||||
|
|
||||||
* [参考文献](#参考文献)
|
* [参考文献](#参考文献)
|
||||||
<!-- GFM-TOC -->
|
<!-- GFM-TOC -->
|
||||||
@ -1439,6 +1441,136 @@ if __name__ == '__main__':
|
|||||||
else:
|
else:
|
||||||
print ('wrong')
|
print ('wrong')
|
||||||
```
|
```
|
||||||
|
# 18.2 删除链表中重复的结点
|
||||||
|
|
||||||
|
[NowCoder](https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef?tpId=13&tqId=11209&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
||||||
|
|
||||||
|
## 题目描述
|
||||||
|
|
||||||
|
<div align="center"> <img src="../pics//8433fbb2-c35c-45ef-831d-e3ca42aebd51.png" width="500"/> </div><br>
|
||||||
|
|
||||||
|
## 解题描述
|
||||||
|
|
||||||
|
```java
|
||||||
|
public ListNode deleteDuplication(ListNode pHead) {
|
||||||
|
if (pHead == null || pHead.next == null)
|
||||||
|
return pHead;
|
||||||
|
ListNode next = pHead.next;
|
||||||
|
if (pHead.val == next.val) {
|
||||||
|
while (next != null && pHead.val == next.val)
|
||||||
|
next = next.next;
|
||||||
|
return deleteDuplication(next);
|
||||||
|
} else {
|
||||||
|
pHead.next = deleteDuplication(pHead.next);
|
||||||
|
return pHead;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
# class ListNode:
|
||||||
|
# def __init__(self, x):
|
||||||
|
# self.val = x
|
||||||
|
# self.next = None
|
||||||
|
class Solution:
|
||||||
|
def deleteDuplication(self, pHead):
|
||||||
|
# write code here
|
||||||
|
if not pHead or not pHead.next:
|
||||||
|
return pHead
|
||||||
|
next = pHead.next
|
||||||
|
if pHead.val == next.val:
|
||||||
|
while next and pHead.val == next.val:
|
||||||
|
next = next.next
|
||||||
|
return self.deleteDuplication(next)
|
||||||
|
else:
|
||||||
|
pHead.next = self.deleteDuplication(pHead.next)
|
||||||
|
return pHead
|
||||||
|
```
|
||||||
|
|
||||||
|
# 19. 正则表达式匹配
|
||||||
|
|
||||||
|
[NowCoder](https://www.nowcoder.com/practice/45327ae22b7b413ea21df13ee7d6429c?tpId=13&tqId=11205&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
||||||
|
|
||||||
|
## 题目描述
|
||||||
|
|
||||||
|
请实现一个函数用来匹配包括 '.' 和 '\*' 的正则表达式。模式中的字符 '.' 表示任意一个字符,而 '\*' 表示它前面的字符可以出现任意次(包含 0 次)。
|
||||||
|
|
||||||
|
在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串 "aaa" 与模式 "a.a" 和 "ab\*ac\*a" 匹配,但是与 "aa.a" 和 "ab\*a" 均不匹配。
|
||||||
|
|
||||||
|
## 解题思路
|
||||||
|
|
||||||
|
应该注意到,'.' 是用来当做一个任意字符,而 '\*' 是用来重复前面的字符。这两个的作用不同,不能把 '.' 的作用和 '\*' 进行类比,从而把它当成重复前面字符一次。
|
||||||
|
|
||||||
|
```java
|
||||||
|
public boolean match(char[] str, char[] pattern) {
|
||||||
|
|
||||||
|
int m = str.length, n = pattern.length;
|
||||||
|
boolean[][] dp = new boolean[m + 1][n + 1];
|
||||||
|
|
||||||
|
dp[0][0] = true;
|
||||||
|
for (int i = 1; i <= n; i++)
|
||||||
|
if (pattern[i - 1] == '*')
|
||||||
|
dp[0][i] = dp[0][i - 2];
|
||||||
|
|
||||||
|
for (int i = 1; i <= m; i++)
|
||||||
|
for (int j = 1; j <= n; j++)
|
||||||
|
if (str[i - 1] == pattern[j - 1] || pattern[j - 1] == '.')
|
||||||
|
dp[i][j] = dp[i - 1][j - 1];
|
||||||
|
else if (pattern[j - 1] == '*')
|
||||||
|
if (pattern[j - 2] == str[i - 1] || pattern[j - 2] == '.') {
|
||||||
|
dp[i][j] |= dp[i][j - 1]; // a* counts as single a
|
||||||
|
dp[i][j] |= dp[i - 1][j]; // a* counts as multiple a
|
||||||
|
dp[i][j] |= dp[i][j - 2]; // a* counts as empty
|
||||||
|
} else
|
||||||
|
dp[i][j] = dp[i][j - 2]; // a* only counts as empty
|
||||||
|
|
||||||
|
return dp[m][n];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
class Solution:
|
||||||
|
# s, pattern都是字符串
|
||||||
|
def match(self, s, pattern):
|
||||||
|
# write code here
|
||||||
|
if (len(s) == 0 and len(pattern) == 0):
|
||||||
|
return True
|
||||||
|
if (len(s) > 0 and len(pattern) == 0):
|
||||||
|
return False
|
||||||
|
if (len(pattern) > 1 and pattern[1] == '*'):
|
||||||
|
if (len(s) > 0 and (s[0] == pattern[0] or pattern[0] == '.')):
|
||||||
|
return (self.match(s, pattern[2:]) or self.match(s[1:], pattern[2:]) or self.match(s[1:], pattern))
|
||||||
|
else:
|
||||||
|
return self.match(s, pattern[2:])
|
||||||
|
if (len(s) > 0 and (pattern[0] == '.' or pattern[0] == s[0])):
|
||||||
|
return self.match(s[1:], pattern[1:])
|
||||||
|
return False
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
import re
|
||||||
|
class Solution:
|
||||||
|
# s, pattern都是字符串
|
||||||
|
def match(self, s, pattern):
|
||||||
|
# write code here
|
||||||
|
if s == None or pattern == None:
|
||||||
|
return False
|
||||||
|
if s == "" and pattern == "":
|
||||||
|
return True
|
||||||
|
if pattern == "" and s != "":
|
||||||
|
return False
|
||||||
|
|
||||||
|
res = re.compile(str(pattern)+"$")
|
||||||
|
result = res.match(str(s))
|
||||||
|
|
||||||
|
if result is None:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
```
|
||||||
|
|
||||||
# 参考文献
|
# 参考文献
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user