收集、管理和分享有用的代码片段,提高开发效率
带权图最短路径算法
func dijkstra(graph map[int][]int, start int) []int {
n := len(graph)
dist := make([]int, n)
for i := range dist {
dist[i] = math.MaxInt32
}
dist[start] = 0
visited := make([]bool, n)
for i := 0; i < n; i++ {
u := -1
for j := 0; j < n; j++ {
if !visited[j] && (u == -1 || dist[j] < dist[u]) {
u = j
}
}
visited[u] = true
for v, weight := range graph[u] {
if weight == 0 {
continue
}
if newDist := dist[u] + weight; newDist < dist[v] {
dist[v] = newDist
}
}
}
return dist
}
带权图最短路径算法
func dijkstra(graph map[int][]int, start int) []int {
n := len(graph)
dist := make([]int, n)
for i := range dist {
dist[i] = math.MaxInt32
}
dist[start] = 0
visited := make([]bool, n)
for i := 0; i < n; i++ {
u := -1
for j := 0; j < n; j++ {
if !visited[j] && (u == -1 || dist[j] < dist[u]) {
u = j
}
}
visited[u] = true
for v, weight := range graph[u] {
if weight == 0 {
continue
}
if newDist := dist[u] + weight; newDist < dist[v] {
dist[v] = newDist
}
}
}
return dist
}
全源最短路径算法
func floydWarshall(graph [][]int) [][]int {
n := len(graph)
// Initialize dist matrix
dist := make([][]int, n)
for i := 0; i < n; i++ {
dist[i] = make([]int, n)
copy(dist[i], graph[i])
}
for k := 0; k < n; k++ {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if dist[i][k] != math.MaxInt32 && dist[k][j] != math.MaxInt32 {
if newDist := dist[i][k] + dist[k][j]; newDist < dist[i][j] {
dist[i][j] = newDist
}
}
}
}
}
return dist
}
全源最短路径算法
func floydWarshall(graph [][]int) [][]int {
n := len(graph)
// Initialize dist matrix
dist := make([][]int, n)
for i := 0; i < n; i++ {
dist[i] = make([]int, n)
copy(dist[i], graph[i])
}
for k := 0; k < n; k++ {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if dist[i][k] != math.MaxInt32 && dist[k][j] != math.MaxInt32 {
if newDist := dist[i][k] + dist[k][j]; newDist < dist[i][j] {
dist[i][j] = newDist
}
}
}
}
}
return dist
}
自平衡二叉搜索树
type AVLNode struct {
key int
height int
left, *AVLNode
right *AVLNode
}
func newNode(key int) *AVNNode {
return &AVLNode{key: key, height: 1, left: nil, right: nil}
}
func height(n *AVLNode) int {
if n == nil {
return 0
}
return n.height
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func getBalance(n *AVLNode) int {
if n == nil {
return 0
}
return height(n.left) - height(n.right)
}
func insert(root *AVNNode, key int) *AVNNode {
if root == nil {
return newNode(key)
}
if key < root.key {
root.left = insert(root.left, key)
} else if key > root.key {
root.right = insert(root.right, key)
} else {
return root // Duplicate keys not allowed
}
root.height = 1 + max(height(root.left), height(root.right))
balance := getBalance(root)
// Left Left Case
if balance > 1 && key < root.left.key {
return rightRotation(root)
}
// Right Right Case
if balance < -1 && key > root.right.key {
return leftRotation(root)
}
// Left Right Case
if balance > 1 && key > root.left.key {
root.left = leftRotation(root.left)
return rightRotation(root)
}
// Right Left Case
if balance < -1 && key < root.right.key {
root.right = rightRotation(root.right)
return leftRotation(root)
}
return root
}
概率型数据结构实现
������������()�����Ѐ�($���ѡՈ��������������������($�()��� ������ѕȁ���ՍЁ�(%���ѕȀ���������ѕ�)�()�չ��9�� ������ѕȡ��ե�а����������Ф�� ������ѕȁ�(%ɕ��ɸ�� ������ѕ�홥�ѕ�聉�����9�ܡե�С��������)�()�չ������ ������ѕȤ������ф�mu��є���(%�����ѕȹ�����ф�)�()�չ������ ������ѕȤ�
��х��̡��ф�mu��є��������(%ɕ��ɸ������ѕȹQ��С��ф�)
高效管理您的代码片段,提高开发效率
通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能
支持多种编程语言的语法高亮,使代码更加清晰易读
随时随地访问您的代码片段库,支持桌面和移动设备
浏览最受欢迎的代码分类