leetcode总结
头发结冰海鲜过敏的鱼
这个作者很懒,什么都没留下…
展开
-
python 常用库
1. random 产生随机数import randomrandom.choice(list1) 从list中随机返回一个数random.random() 产生0 - 1 的随机数random.uniform(a, b) 产生 a- b 之间的随机数2.Queueimport queuea = queue.Queue()a.put()a.get()3.Heapimport heapqheapq.heappush(queue, item)heap..原创 2020-09-30 06:45:02 · 160 阅读 · 0 评论 -
图中的复杂算法
1.求最小距离 Dijkstra algorithm其实就是一个BFS问题,只是BFS运行的时候每次是入queue,Dijkstra每次是入heap(每次出最小的)。2.求一个线性序列 满足若有 U 到 V的edge, U一定在V之前 Topology sort其实就是个DFS问题,只是每次遇到新节点的时候,不print,而是入stack,最后输出stack。note:必须是有向无环的!!! 有环是无法得到序列的。3.强连接元素4. 最小生成树寻找能将所有ver...原创 2020-09-08 07:41:02 · 321 阅读 · 0 评论 -
Basic sorting and searching
1. Sequential Search遍历一遍数组,发现相等则返回:复杂度 = O(n), 平均时间为 n/22. Binary Search每次通过比较target和中间值List[middle]的大小关系改变start指针和end指针,每次都丢弃剩余数组的1/2。复杂度O(logN)while Start < End:if 中间值==target 则返回...原创 2019-07-08 21:21:42 · 194 阅读 · 0 评论 -
leetcode 33. Search in Rotated Sorted Array
In order to help me understand all the possible situations I should consider, I just draw a picture to make everything easier.So acutally there are four situations we should consider, we should first...原创 2019-07-15 14:50:37 · 127 阅读 · 0 评论 -
dynamic programming 常见动态规划题目(持续更新)
动态规划题目:最少硬币问题 Makechanges usingthe fewest coins: 0-1背包 0-1 package 最长回文子串 longest palindromic substring 最长公共子串 longest common subsequence 最长公共子序列 longest common substring 最长连续子串Maximum Subar...原创 2019-06-24 16:18:13 · 541 阅读 · 0 评论 -
Everything about graph(algorithms in python)
1. DefinitionsVertex: 角, or Node.Edge: 边, or Edge.Weight: 权重,the cost to go from one node to another, following an edge.2.adjacent matrix 邻接矩阵使用一个二维数组来表达一个图:图中行跟列都表示节点,第v0行第v1列:表示从v0到v1...原创 2019-07-29 16:54:01 · 152 阅读 · 0 评论 -
Tree, priority queue and BST (python)
一.建立树结构:在自定义的二叉树内,我们需要保存当前节点的值,其左子树和其右子树。同时还需定义插入函数,注意插入函数定义时需要考虑是否当前节点已经有了左子树或右子树,若已经存在,则应该在插入新节点后接上原子树。class BinaryTree: def __init__(self, root):# initialization self.key = root ...原创 2019-07-22 16:02:11 · 254 阅读 · 0 评论 -
Reverse a single linked list with 0(n) time complexity
LeetCode 92Reverse a linked list from positionmton. Do it in one-pass. 注意必须使用O(n)的复杂度来实现Note:1 ≤m≤n≤ length of list.Example:Input: 1->2->3->4->5->NULL, m = 2, n = 4Ou...原创 2019-08-05 17:46:32 · 170 阅读 · 0 评论 -
Python binary and decimal transformation.
将 一个十进制的数转换为 2 进制:a = 10aB = bin(a)但是它不会自动变为32位,而是在最高位不是0的位置停下。将一个2进制数转化为10进制:AD = int(aB, 2)#第二个位置的2表示base是二进制(是从二进制转换成10进制的)...原创 2019-09-15 11:06:14 · 220 阅读 · 0 评论