随笔分类 -  数据结构

摘要:思路: 二分+递归 对k二分,分别比较a,b k/2位置的大小,小的那一部分就可以删除了,再更新k basecase是 k为1或者 其中一个数组删完了 package main import "fmt" func findKth(a, b []int, aleft, bleft, k int) in 阅读全文
posted @ 2022-09-17 15:08 Notomato 阅读(25) 评论(0) 推荐(0) 编辑
摘要:Golang代码 package main import ( "fmt" "strconv" "strings" ) type Treenode struct { Val int Left, Right *Treenode } func Serialization(node *Treenode) s 阅读全文
posted @ 2022-09-16 15:52 Notomato 阅读(24) 评论(0) 推荐(0) 编辑
摘要:package main import "fmt" var swapcnt int func main() { arr := []int{2, 3, 4, 5, 1} //idx := Swap(arr, 0, len(arr)) //fmt.Println(idx, arr) myquickSor 阅读全文
posted @ 2022-08-31 13:14 Notomato 阅读(43) 评论(0) 推荐(0) 编辑
摘要:二分图最大匹配数量,匈牙利算法求解 python,本质上是找增广回路 """ # File : hungary.py # Time :2022/8/28 21:08 # Author :notomato # Description: # """ ''' ''' # 六个点 # 第i个坐标存他相邻的点 阅读全文
posted @ 2022-08-29 12:55 Notomato 阅读(110) 评论(0) 推荐(0) 编辑
摘要:思路: 先转逆波兰,再求值 逆波兰求值的思路是,碰到数字入栈,碰到运算符则取栈顶两元素运算后入栈,最终栈内数字即为答案。 中缀表达式转为后缀表达式,即逆波兰表达式方法:对于中缀表达式的每个元素,遇到数字则直接加到结果上面,遇到操作符,若栈空或者当前操作符不小于栈顶操作符优先级(乘除大于加减),则入栈 阅读全文
posted @ 2022-08-27 19:01 Notomato 阅读(102) 评论(0) 推荐(0) 编辑
摘要:Python版本 class Trie: def __init__(self): self.children = defaultdict(Trie) self.word = "" self.is_word = False def insert(self, word): cur = self for 阅读全文
posted @ 2022-08-20 17:10 Notomato 阅读(26) 评论(0) 推荐(0) 编辑
摘要:Python版本 class UF: parent = {} size = {} cnt = 0 def __init__(self, M): # 初始化 parent,size 和 cnt # self.parent = {i for i in range(n)} def find(self, x 阅读全文
posted @ 2022-08-20 17:08 Notomato 阅读(5) 评论(0) 推荐(0) 编辑
摘要:根据先序中序->后序,先序后序->中序,中序后序->先序,构建二叉树的方法,注意根据先序后序构建中序并不是唯一的,下面的代码以字符串模拟,若要还原树结构,将关键部分换成node即可 package main import "fmt" func prein2post(preorder string, 阅读全文
posted @ 2022-08-15 15:11 Notomato 阅读(47) 评论(0) 推荐(0) 编辑
摘要:题目:求小于n的所有质数 package main import "fmt" func main() { primes := []int{} n := 1010 notprime := make(map[int]bool) for i := 2; i <= n; i++ { if !notprime 阅读全文
posted @ 2022-07-16 16:59 Notomato 阅读(61) 评论(0) 推荐(0) 编辑
摘要:介绍 Go的list是一个双向链表,链表可以高效的删除插入元素,很多场景是需要用到这个结构的,比如cache 使用 list在包container/list下,可以通过NeW()初始化或者var声明 两种方法如下 mylist := list.New() var mylist list.List 常 阅读全文
posted @ 2022-05-20 10:51 Notomato 阅读(1094) 评论(0) 推荐(0) 编辑
摘要:本文主要介绍Golang的堆排序使用方法,通过container/heap实现堆相关的操作 堆因为其插入删除的操作时间复杂度低,因此优先队列通常是靠堆来实现的。Go的堆使用要实现heap包下的Interface接口 type Interface interface { sort.Interface 阅读全文
posted @ 2022-05-19 17:35 Notomato 阅读(165) 评论(0) 推荐(0) 编辑
摘要:python heapq相关操作 heapq 的一些常用方法: heapify(list)将列表转换为小根堆的数据结构。 heappush(heap, x),将 x 加入堆中。 heappop(heap),从堆中弹出最小的元素。 heapreplace(heap, x),弹出最小的元素,并将 x 加 阅读全文
posted @ 2022-05-19 16:50 Notomato 阅读(862) 评论(0) 推荐(0) 编辑
摘要:sort包简介 官方文档 Golang的sort包用来排序,二分查找等操作。本文主要介绍sort包里常用的函数,通过实例代码来快速学会使用sort包 sort包内置函数 sort.Ints(x []int) ints := []int{1, 4, 3, 2} fmt.Printf("%v\n", i 阅读全文
posted @ 2022-05-16 17:18 Notomato 阅读(503) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示