- 博客(31)
- 收藏
- 关注
原创 Dijkstra算法
Dijkstra算法"""Dijkstra1算法采用邻接表,边数e,点数v复杂度O(eloge)"""def Dijkstra1(graph, start, end): m = len(graph) S, T = [], [i for i in range(m)] dis = [float('inf') for _ in range(m)] hq = [] heapq.heappush(hq, (0, start)) while hq:
2021-09-08 19:37:47
247
原创 匈牙利算法,KM算法Python实现
匈牙利算法# -*- coding: utf-8 -*-"""匈牙利算法O(E*V)"""def Hungarian(l_node, visited): for r_node in graph[l_node]: if r_node not in T: S[l_node] = r_node T[r_node] = l_node return True else:
2021-09-08 16:44:52
1600
原创 Git学习
Git学习Git安装后,设置用户名和邮箱:$ git config --global user.name "Your Name"$ git config --global user.email "email@example.com"创建版本库:$ mkdir learngit$ cd learngit$ pwd #pwd命令用于显示当前目录/e/python_crash_course/learninggit通过git init命令把这个目录变成Git可以管理的仓库:$ git ini
2021-03-12 14:01:48
282
原创 Gurobi Class3笔记
课程三:Callback使用方法3.1 Callback使用方法3.11 Callback函数Callback为用户在求解模型时提供了高级控制功能,如在求解过程中获取信息,终止优化,加入额外约束条件和加入自己开发的算法\定义callback: def name(model, where): 调用callback: m.optimize(callback函数名)参数: where:回调函数触发点 what:获取何种信息,what能获取什么取决于where3.12 cbGet(what):查询
2021-01-18 14:21:30
1959
原创 Gurobi Class2笔记
Gurobi Class2:Gurobi功能和操作进阶2参数2.1参数类别1)Termination停止参数:TimeLimit设定时间;SolutionLimit设定MIP可行解数量2)Tolerances容差参数:MIPGap设定MIP的gap值;FeasibilityTol设定精度3)Simplex单纯性参数:InfUnbdInfo控制是否获取不可行或无界模型的额外信息4)Barrier障碍法参数:QCPDual控制是否获取二次模型的对偶值5)Mip混合整数参数:BranchDir(ec
2021-01-12 14:36:46
5826
1
原创 Gurobi Class1笔记
Gurobi Class1:Gurobi基本操作from gurobipy import *#tuplelistCities = [('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D'), ('C', 'D')]Routes = gurobipy.tuplelist(Cities)print(Routes)print(Routes.select('A', '*'))'''<gurobi.tuplelist (5 tuples, 2 values
2021-01-12 13:54:15
1087
1
原创 二分法Lintcode
875爱吃香蕉的珂珂class Solution: def minEatingSpeed(self, piles: List[int], H: int) -> int: # 暴力 K_max = max(piles) for i in range(1, K_max + 1): if self.canEatAll(piles, i) <= H: return i #
2020-08-20 11:58:54
148
原创 二叉树,树的遍历
树的遍历深度优先搜索前序遍历class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: #递归解法 #result = [] #def preorder(root): # if not root: # return # result.append(root.val)
2020-08-19 13:57:10
139
原创 Leetcode 542 01矩阵
542 01矩阵class Solution: def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]: m, n = len(matrix), len(matrix[0]) dist = [[0] * n for _ in range(m)] zeroes_pos = [(i, j) for i in range(m) for j in range(n) if m
2020-08-16 20:35:11
95
原创 Leetcode 733图像渲染
733图像渲染class Solution: def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]: target = image[sr][sc] m, n = len(image), len(image[0]) q = collections.deque([(sr, sc)]) image[
2020-08-15 18:12:35
121
原创 Leetcode 394字符串解码
394字符串解码class Solution: def decodeString(self, s): stack = [] for string in s: if string != ']': stack.append(string) else: tmp = '' while stack[-1] != '[':
2020-08-15 18:11:12
138
原创 Leetcode 225用队列实现栈
225用队列实现栈class MyStack: def __init__(self): """ Initialize your data structure here. """ self.q1 = [] self.q2 = [] self.tp = None def push(self, x: int) -> None: """ Push element
2020-08-15 18:09:17
203
原创 Leetcode 232用栈实现队列
232用栈实现队列class MyQueue: def __init__(self): """ Initialize your data structure here. """ self.items = [] def push(self, x: int) -> None: """ Push element x to the back of queue. """
2020-08-15 18:07:49
135
原创 Leetcode 494目标和
494目标和class Solution: def findTargetSumWays(self, nums: List[int], S: int) -> int: d = {} def dfs(cur, i, d): if i < len(nums) and (cur, i) not in d: d[(cur, i)] = dfs(cur + nums[i], i+1, d) + dfs(cur
2020-08-15 18:06:00
169
原创 Leetcode 133克隆图
133克隆图"""# Definition for a Node.class Node: def __init__(self, val = 0, neighbors = []): self.val = val self.neighbors = neighbors"""class Solution: def cloneGraph(self, node: 'Node') -> 'Node': if not node:
2020-08-15 18:04:20
129
原创 Leetcode 150逆波兰表达式求值
150逆波兰表达式求值class Solution(object): def evalRPN(self, tokens): """ :type tokens: List[str] :rtype: int """ stack =[] for token in tokens: if token not in '+-*/': stack.append(t
2020-08-15 18:02:16
123
原创 Leetcode 739. 每日温度
739. 每日温度class Solution: def dailyTemperatures(self, T): stack = [] n = len(T) outputlist = [0] * n for i in range(n): while stack != [] and T[i] > T[stack[-1]]: top =stack.pop()
2020-08-15 18:00:41
95
原创 Leetcode 20有效的括号
20有效的括号class Solution: def isValid(self, s: str) -> bool: if not s: return True stack = collections.deque() for i in s: if i in '([{': stack.append(i) else:
2020-08-15 17:58:05
83
原创 Leetcode 155最小栈
155最小栈class MinStack(object): def __init__(self): """ initialize your data structure here. """ self.items = [] self.minitems = [] def push(self, x): """ :type x: int :rtype: None
2020-08-15 17:55:19
112
原创 Lintcode多重背包问题
多重背包问题#背包问题VIIclass Solution: """ @param n: the money of you @param prices: the price of rice[i] @param weight: the weight of rice[i] @param amounts: the amount of rice[i] @return: the maximum weight """ def backPackVII(se
2020-08-05 20:15:25
210
原创 Lintcode完全背包问题
完全背包问题完全背包:枚举每件物品取0,1,2,3 … m / A[i] 件,dp[i][j] = max(dp[i - 1][j],dp[i - 1][j - k * A[i]] + k * V[i])#背包问题IIIclass Solution: """ @param A: an integer array @param V: an integer array @param m: An integer @return: an array """
2020-08-05 14:36:58
143
原创 Lintcode 0-1背包问题
背包问题0-1背包问题状态转移方程dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]),i代表第几个物品,j代表背包容量,w[i - 1],v[i - 1]代表第i个物品的重量和价值。#背包问题Iclass Solution: """ @param m: An integer m denotes the size of a backpack @param A: Given n items with s
2020-08-05 00:01:46
387
原创 Leetcode279完全平方数
Leetcode279完全平方数class Solution: def numSquares(self, n: int) -> int: square_nums = [a * a for a in range(1, int(n**0.5+1))] level = 0 queue = [n] while queue: level += 1 next_que = set()
2020-08-04 12:10:39
170
原创 Leetcode752 打开转盘锁
Leetcode752 打开转盘锁class Solution: def openLock(self, deadends: List[str], target: str) -> int: def neighbor(node): y = list() for i in range(4): for j in (-1, 1): update = (int(nod
2020-08-04 12:08:59
129
原创 Leetcode200岛屿数量
Leetcode200岛屿数量bfsclass Solution: def numIslands(self, grid): if not grid or not grid[0]: return 0 self.m, self.n = len(grid), len(grid[0]) islands = 0 for row in range(self.m): for col in range
2020-08-04 12:07:30
142
原创 Leetcode286墙与门
286墙与门#286墙与门class Solution: def wallsAndGates(self, rooms): EMPTYROOM = 2**31 - 1 door = 0 neighbors = [[-1,0], [1,0], [0,-1], [0,1]] if not rooms: return row = len(rooms) col = len(rooms[0
2020-08-04 12:03:46
200
原创 Leetcode346数据流中的移动平均值
346数据流中的移动平均值#346数据流中的移动平均值from collections import dequefrom pythonds import Dequeclass MovingAverage(): def __init__(self, size): self.queue = Deque() self.maxsize = size self.count = 0 def nxt(self, val): se
2020-08-04 12:02:12
146
原创 Leetcode设计循环队列
设计循环队列class MyCircularQueue: def __init__(self, k: int): """ Initialize your data structure here. Set the size of the queue to be k. """ self.len = k self.values = [-1] * self.len self.front = 0
2020-08-04 12:00:34
183
原创 力扣:删除排序数组中的重复项
80. 删除排序数组中的重复项 IIclass Solution: def removeDuplicates(self, nums: List[int]) -> int: # 双指针O(N) length = len(nums) slow, fast = 1, 1 count = 1 while fast < length: if nums[fast] == nums[fast -
2020-07-22 22:17:08
180
原创 力扣Leetcode 118/119杨辉三角I/II
118.杨辉三角Iclass Solution: def generate(self, numRows: int) -> List[List[int]]: # 动态规划 O(N^2) triangle = [] for row_num in range(numRows): row = [None for _ in range(row_num + 1)] row[0], row[-1] = 1,
2020-07-22 15:43:02
100
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人