defquicksort(nums, low, high): if low >= high: return base = nums[low] r = low l = high while r < l: while nums[l] >= base and l > r: l -= 1 while nums[r] <= base and r < l: r += 1 nums[r], nums[l] = nums[l], nums[r] nums[low], nums[r] = nums[r], nums[low] quicksort(nums, low, r-1) quicksort(nums, r+1, high)
defquick_pow(x, n, m): if n == 0: return1 if n%2 == 0: return quick_pow(x, n // 2, m) ** 2 % m else: return quick_pow(x, n-1, m) * x % m defquick_pow2(x, n, m): if n == 0: return1 ans = 1 base = x while n > 0: if n % 2 == 1: ans *= base % m base *= base % m n //= 2 return ans % m
# 初始化路由矩阵 R = [[0for i inrange(node_number)] for j inrange(node_number)] for i inrange(node_number): for j inrange(node_number): if W[i][j] > 0: R[i][j] = j+1 else: R[i][j] = 0 # 查看初始化的路由矩阵 for row in R: print(row)
# 循环求W_n和R_n for k inrange(node_number): for i inrange(node_number): for j inrange(node_number): if W[i][k] > 0and W[k][j] > 0and (W[i][k] + W[k][j] < W[i][j] or W[i][j] == -1): W[i][j] = W[i][k] + W[k][j] R[i][j] = k+1 print("第%d次循环:" % (k+1)) print("距离矩阵:") for row in W: print(row) print("路由矩阵:") for row in R: print(row)
classSolution: defpartition(self, s: str) -> List[List[str]]: n = len(s) res = [] tmp = [] @cache defcheck(i, j): if i >= j: return1 if s[i] == s[j]: return check(i+1, j-1) else: return0
defdfs(i): if i == n: res.append(tmp[:]) return for j inrange(i, n): if check(i, j): tmp.append(s[i:j+1]) dfs(j+1) tmp.pop() dfs(0) check.cache_clear() return res