文章目录
一,编辑距离
1,程序简介
- 给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。
- 你可以对一个单词进行如下三种操作:
- 插入一个字符
- 删除一个字符
- 替换一个字符
示例 1:
- 输入:word1 = “horse”, word2 = “ros”
- 输出:3
- 解释:horse -> rorse (将 ‘h’ 替换为 ‘r’)rorse -> rose (删除 ‘r’)rose -> ros (删除 ‘e’)
示例 2:
- 输入:word1 = “intention”, word2 = “execution”
- 输出:5
- 解释:intention -> inention (删除 ‘t’)inention -> enention (将 ‘i’ 替换为 ‘e’)enention -> exention (将 ‘n’ 替换为 ‘x’)exention -> exection (将 ‘n’ 替换为 ‘c’)exection -> execution (插入 ‘u’)
提示:
0 <= word1.length, word2.length <= 500
word1 和 word2 由小写英文字母组成
以下程序实现了这一功能,请你填补空白处内容:
class Solution(object):
def minDistance(self, word1, word2):
ls_1, ls_2 = len(word1), len(word2)
dp = list(range(ls_1 + 1))
for j in range(1, ls_2 + 1):
pre = dp[0]
dp[0] = j
for i in range(1, ls_1 + 1):
temp = dp[i]
if word1[i - 1] == word2[j - 1]:
dp[i] = pre
else:
____________________________
pre = temp
return dp[ls_1]
if __name__ == '__main__':
s = Solution()
print (s.minDistance("horse","ros"))
print (s.minDistance("intention","execution"))
2,程序代码
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 26 10:19:49 2021
@author: 小梁aixj
"""
class Solution(object):
def minDistance(self, word1, word2):
ls_1, ls_2 = len(word1), len(word2)
dp = list(range(ls_1 + 1))
for j in range(1, ls_2 + 1):
pre = dp[0]
dp[0] = j
for i in range(1, ls_1 + 1):
temp = dp[i]
if word1[i - 1] == word2[j - 1]:
dp[i] = pre
else:
dp[i] = min(pre + 1, dp[i] + 1, dp[i - 1] + 1)
pre = temp
return dp[ls_1]
if __name__ == '__main__':
s = Solution()
print (s.minDistance("horse","ros"))
print (s.minDistance("intention","execution"))
3,运行结果
二,最大矩形
1,程序简介
- 给定一个仅包含 0 和 1 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。
示例 1:
- 输入:matrix = [[“1”,“0”,“1”,“0”,“0”],[“1”,“0”,“1”,“1”,“1”],[“1”,“1”,“1”,“1”,“1”],[“1”,“0”,“0”,“1”,“0”]]
- 输出:6
- 解释:最大矩形如上图所示。
示例 2:
- 输入:matrix = []
- 输出:0
示例 3:
- 输入:matrix = [[“0”]]
- 输出:0
示例 4:
- 输入:matrix = [[“1”]]
- 输出:1
示例 5:
- 输入:matrix = [[“0”,“0”]]
- 输出:0
提示:
rows == matrix.length
cols == matrix[0].length
0 <= row, cols <= 200
matrix[i][j] 为 ‘0’ 或 ‘1’
以下程序实现了这一功能,请你填补空白处内容:
class Solution(object):
def maximalRectangle(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
if matrix is None or len(matrix) == 0:
return 0
ls_row, ls_col = len(matrix), len(matrix[0])
left, right, height = [0] * ls_col, [ls_col] * ls_col, [0] * ls_col
maxA = 0
for i in range(ls_row):
curr_left, curr_right = 0, ls_col
for j in range(ls_col):
if matrix[i][j] == '1':
height[j] += 1
else:
height[j] = 0
for j in range(ls_col):
if matrix[i][j] == '1':
left[j] = max(left[j], curr_left)
else:
left[j], curr_left = 0, j + 1
_______________________
for j in range(ls_col):
maxA = max(maxA, (right[j] - left[j]) * height[j])
return maxA
# %%
s = Solution()
matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
print(s.maximalRectangle(matrix))
2,程序代码
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 26 10:19:49 2021
@author: 小梁aixj
"""
class Solution(object):
def maximalRectangle(self, matrix):
if matrix is None or len(matrix) == 0:
return 0
ls_row, ls_col = len(matrix), len(matrix[0])
left, right, height = [0] * ls_col, [ls_col] * ls_col, [0] * ls_col
maxA = 0
for i in range(ls_row):
curr_left, curr_right = 0, ls_col
for j in range(ls_col):
if matrix[i][j] =='1':
height[j] += 1
else:
height[j] = 0
for j in range(ls_col):
if matrix[i][j] == '1':
left[j] = max(left[j], curr_left)
else:
left[j], curr_left = 0, j + 1
for j in range(ls_col - 1, -1, -1):
if matrix[i][j] == '1':
right[j] = min(right[j], curr_right)
else:
right[j], curr_right = ls_col, j
for j in range(ls_col):
maxA = max(maxA, (right[j] - left[j]) * height[j])
return maxA
# %%
s = Solution()
matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
print(s.maximalRectangle(matrix))
3,运行结果
三,反转链表 ||
1,程序简介
- 给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。
示例 1:
- 输入:head = [1,2,3,4,5], left = 2, right = 4
- 输出:[1,4,3,2,5]
示例 2:
- 输入:head = [5], left = 1, right = 1
- 输出:[5]
提示:
链表中节点数目为 n
1 <= n <= 500
-500 <= Node.val <= 500
1 <= left <= right <= n
进阶: 你可以使用一趟扫描完成反转吗?
2,程序代码
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 26 10:19:49 2021
@author: 小梁aixj
"""
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class LinkList:
def __init__(self):
self.head=None
def initList(self, data):
self.head = ListNode(data[0])
r=self.head
p = self.head
for i in data[1:]:
node = ListNode(i)
p.next = node
p = p.next
return r
def convert_list(self,head):
ret = []
if head == None:
return
node = head
while node != None:
ret.append(node.val)
node = node.next
return ret
class Solution(object):
def reverseBetween(self, head, m, n):
if m == n:
return head
split_node, prev, curr = None, None, head
count = 1
while count <= m and curr is not None:
if count == m:
split_node = prev
prev = curr
curr = curr.next
count += 1
tail= prev
while curr is not None and count <= n:
next_temp = curr.next
curr.next = prev
prev = curr
curr = next_temp
count += 1
if split_node is not None:
split_node.next = prev
if tail is not None:
tail.next = curr
if m == 1:
return prev
return head
# %%
l = LinkList()
list1 = [1,2,3,4,5]
l1 = l.initList(list1)
left = 2
right = 4
s = Solution()
print(l.convert_list(s.reverseBetween(l1, left, right)))#[1,4,3,2,5]