LeetCode 71-90題
阿新 • • 發佈:2022-01-13
本部落格記錄的是 LeetCode 71 到 90 題的題解
之前很少使用的語法
# 72. Edit Distance
a += ' '
a = ' ' + a
71. Simplify Path
雙指標演算法
class Solution: def simplifyPath(self, path: str) -> str: i, j = 0, 0 n = len(path) dir_names = [] while i < n: if path[i] == '/': while i < n and path[i] == '/': i += 1 else: j = i + 1 while j < n and path[j] != '/': j += 1 dir_names.append(path[i:j]) i = j new_dir = [] for name in dir_names: if name == '.': continue elif name == '..': if new_dir: new_dir.pop() else: new_dir.append(name) return '/' + '/'.join(new_dir)
72. Edit Distance
入門動態規劃,最短編輯距離
# Dp class Solution: def minDistance(self, a: str, b: str) -> int: # define and initialize n, m = len(a), len(b) a = ' ' + a b = ' ' + b f = [[0] * (m + 1) for i in range(n + 1)] for j in range(0, m + 1): f[0][j] = j for i in range(0, n + 1): f[i][0] = i # dynamic programming for i in range(1, n + 1): for j in range(1, m + 1): if a[i] == b[j]: f[i][j] = f[i - 1][j - 1] else: f[i][j] = min(f[i - 1][j - 1], min(f[i - 1][j], f[i][j - 1])) + 1 return f[n][m]
73. Set Matrix Zeroes
一定遇到原地操作,空間複雜度O(0)的時候,主要是應用好原本的陣列,僅僅開闢常數級別的變數
class Solution: def setZeroes(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """ flag_0_row, flag_0_column = False, False n, m = len(matrix), len(matrix[0]) for i in range(n): for j in range(m): if matrix[i][j] == 0: matrix[i][0] = 0 matrix[0][j] = 0 if i == 0: flag_0_row = True if j == 0: flag_0_column = True for i in range(1, n): if matrix[i][0] == 0: for j in range(m): matrix[i][j] = 0 for j in range(1, m): if matrix[0][j] == 0: for i in range(n): matrix[i][j] = 0 if flag_0_row: for j in range(m): matrix[0][j] = 0 if flag_0_column: for i in range(n): matrix[i][0] = 0
74. Search a 2D Matrix
簡單的二分問題,先 row 二分,然後 col 二分即可,查詢是否存在 target 數值
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
n, m = len(matrix), len(matrix[0])
l, r = 0, n - 1
if matrix[l][0] > target:
return False
else:
# 尋找 matrix[XXX][0] <= target 的最大值
while l < r:
mid = (l + r + 1) // 2
if matrix[mid][0] <= target:
l = mid
else:
r = mid - 1
# 尋找 matrix[x][XXX] <= target 的最大值
x = l
l, r = 0, m - 1
while l < r:
mid = (l + r + 1) // 2
if matrix[x][mid] <= target:
l = mid
else:
r = mid - 1
return matrix[x][l] == target
75. Sort Colors
# 快排
# 或者是直接進行數數
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
cnt, n = [0] * 3, len(nums)
for i in range(n):
cnt[nums[i]] += 1
sum = 0
for k in range(3):
for i in range(sum, sum + cnt[k]):
nums[i] = k
sum += cnt[k]
如果是隻想掃描一遍的話,可以按照雙指標演算法來寫
有 0 就從前往後放,有 2 就從後往前放,剩下的就是 1的地方,不過這樣放置的話,可能會覆蓋一個數組單元,因此需要那一個臨時變數儲存一下子,這裡我使用的 t = nums[n - 1] 來防止 nums[[n - 1]未遍歷前就被覆蓋了
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
i, j = 0, n - 2
t = nums[n - 1]
cnt = 0
pos_0, pos_2 = 0, n - 1
flag = True
while i <= j:
if flag:
if nums[i] == 0:
nums[pos_0] = 0
pos_0 += 1
elif nums[i] == 2:
nums[pos_2] = 2
pos_2 -= 1
flag = False
i += 1
else:
if nums[j] == 0:
nums[pos_0] = 0
pos_0 += 1
flag = True
elif nums[j] == 2:
nums[pos_2] = 2
pos_2 -= 1
j -= 1
if t == 0:
nums[pos_0] = 0
pos_0 += 1
elif t == 2:
nums[pos_2] = 2
pos_2 -= 1
for i in range(pos_0, pos_2 + 1):
nums[i] = 1