avatar
文章
181
标签
35
分类
22
首页
文章
  • 归档
  • 分类
  • 标签
  • 说说
摄影集
友链
留言板
关于我
DeepThinking深思
搜索
首页
文章
  • 归档
  • 分类
  • 标签
  • 说说
摄影集
友链
留言板
关于我

DeepThinking深思

go语言-回调函数(钩子)
发表于2023-05-25|编程语言Golang
在Go语言中,回调函数和钩子函数通常是使用函数类型作为参数传递给函数或方法,以便在特定事件发生时被调用。这种机制非常灵活,可以让你编写出高效的、可复用的代码。 以下是一个简单的例子,展示了如何使用回调函数来实现一个函数,当输出文本时会同时调用传入的回调函数: 123456789101112131415161718package mainimport ( "fmt")func printWithCallback(callback func(string)) { text := "Hello, world!" fmt.Println(text) callback(text)}func main() { callback := func(text string) { fmt.Printf("Printed: %s\n", text) } ...
leetcode 376.摆动序列
发表于2023-05-24|算法LeetCode
376. 摆动序列 - 力扣(Leetcode) 12345678910111213141516171819func wiggleMaxLength(nums []int) int { var count, preDiff, curDiff int count = 1 // 初始化计数为1,至少有一个数字是有效的 if len(nums) < 2 { return count // 如果数组长度小于2,直接返回计数值 } for i := 0; i < len(nums)-1; i++ { curDiff = nums[i+1] - nums[i] // 计算当前数字之间的差值 // 根据差值的正负和前一个差值的正负进行判断 // 如果满足摆动序列的条件,更新前一个差值和计数值 if (curDiff > 0 && preDiff <= 0) || (preDiff >= 0...
leetcode 226. 翻转二叉树
发表于2023-05-23|算法LeetCode
226. 翻转二叉树 - 力扣(Leetcode) 1234567891011121314151617181920212223/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ // 定义将二叉树翻转func invertTree(root *TreeNode) *TreeNode { // 递归终止条件 if root == nil { return nil } // 单个任务逻辑 交换root 下的两个节点,然后在严格按照定义递归调用左右节点 root.Right,root.Left = root.Left,root.Right // 将右子树翻转 invertTree(root.Right) // 将左子树翻转 ...
leetcode 144. 二叉树的前序遍历
发表于2023-05-23|算法LeetCode
144. 二叉树的前序遍历 - 力扣(Leetcode) 记得提前判断是否为空,否则会报找不到内存指针的错误 注意:这里和层序遍历不一样,这里不用使用中间变量lens := stack.len() 来遍历每层,虽然增加了每层遍历依然可以通过,但是没有必要。只有在层序遍历的时候才需要记录每层的信息。leetcode 102. 二叉树的层序遍历 12345678910111213141516171819202122232425262728/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func preorderTraversal(root *TreeNode) []int { stack := list.New() res := []int{} if root ==...
leetcode 102. 二叉树的层序遍历
发表于2023-05-23|算法LeetCode
102. 二叉树的层序遍历 - 力扣(Leetcode) 使用slice123456789101112131415161718192021222324252627282930313233343536373839404142/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func levelOrder(root *TreeNode) [][]int { // 层序遍历 使用size 记录每层数组 queue node 队列 res := make([][]int, 0) queue := make([]*TreeNode, 0) if root != nil { queue = append(queue, root) } else { return res ...
leetcode 1845.座位预约管理系统
发表于2023-05-23|算法LeetCode
1845. 座位预约管理系统 - 力扣(Leetcode) 超时版本123456789101112131415161718192021222324252627282930313233type seat struct { seatId int isFree int // 空}type SeatManager struct { seats map[int]*seat isFrees []int // 可预约的使用list 保存一份,记得被占用的时候,删除该座位,空缺则添加}func Constructor(n int) SeatManager { var a = SeatManager{make(map[int]*seat, n), make([]int, n)} for i := 0; i < n; i++ { id := i + 1 a.seats[id] = &seat{id, 1} a.isFrees[i] = id //...
leetcode 455.分发饼干
发表于2023-05-21|算法LeetCode
455. 分发饼干 - 力扣(Leetcode) https://leiqicn.gitee.io/ 欢迎关注我的博客,定时更新golang 刷题笔记 贪心的策略: 贪心算法一般分为如下四步: 将问题分解为若干个子问题找出适合的贪心策略求解每一个子问题的最优解将局部最优解堆叠成全局最优解 排序,遍历饼干,child胃口初始化idx=0 ,res =0 ;如果没有越界并且饼干大于等于胃口,则childIdx ++;res++ 12345678910111213141516171819func findContentChildren(g []int, s []int) int {    // g 小孩胃口 s 饼干 从大到小排序 排序,并给胃口初始化赋值    sort.Ints(g)    sort.Ints(s)    j := 0    res := 0    // 用小饼干 来满足最小的胃口    for i := 0; i < len(s); i++ {        // 判断越界 饼干大于胃口        if j...
网站无法访问categories?
发表于2023-05-21|综合工具
在package.json中没有放 下边的hexo-generator-category,导致无法生成对应的文件。 可以显示啦
leetcode 706. 设计哈希映射 Golang
发表于2023-05-20|算法LeetCode
706. 设计哈希映射 - 力扣(Leetcode)使用了go 语言的list.List (双向列表),具体如何使用请看文章:Go语言-list.List 使用结构体(非指针)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960// 链地址法var base = 769// base := 769type entry struct { k int v int}type MyHashMap struct { hashMaps []list.List}// hash集合可以使用数组链表;func Constructor() MyHashMap { return MyHashMap{make([]list.List, base)}}// func (this *MyHashMap) hash(key int, value...
leetcode 705. 设计哈希集合
发表于2023-05-19|算法LeetCode
705. 设计哈希集合 - 力扣(Leetcode) 1234567891011121314151617181920212223242526272829303132333435363738394041const base = 769 // 哈希表的长度 质数type MyHashSet struct { data []list.List // 使用链表储存冲突元素}func Constructor() MyHashSet { // 构造函数,返回一个空的哈希集合 return MyHashSet{make([]list.List, base)}}// 哈希函数:对键值取模得到哈希值func (s *MyHashSet) hash(key int) int { return key % base}func (s *MyHashSet) Add(key int) { // 向哈希集合中添加元素 if !s.Contains(key) { //...
1…151617…19
avatar
Lei Qi
记录、总结、复盘、分享
文章
181
标签
35
分类
22
Follow Me
公告

欢迎关注公众号:DeepThinking深思,本站的文章会同步发布在微信公众号,方便推送~

最新文章
GitHub Trending日报(2026年04月10日)2026-04-10
GitHub Trending 日报 - 2026/04/102026-04-10
GitHub Trending 日报 - 2026/04/092026-04-09
GitHub Trending 日报 - 2026/04/082026-04-08
GitHub Trending 日报 - 2026/03/262026-03-26
分类
  • AI2
    • AI工具1
  • 技术栈19
    • DevOps1
    • Docker1
    • Git7
    • Linux5
    • 网络4
    • 自动化1
  • 架构设计3
    • 设计模式3
  • 算法76
    • LeetCode75
    • 速刷记录1
  • 综合48
    • 工具47
    • 比赛1
  • 编程语言33
    • C++1
    • Golang27
    • Python3
    • 前端2
归档
  • 四月 2026 4
  • 三月 2026 20
  • 二月 2026 1
  • 十一月 2025 3
  • 八月 2025 1
  • 七月 2025 5
  • 六月 2025 23
  • 五月 2025 2
网站信息
文章数目 :
181
本站访客数 :
本站总浏览量 :
最后更新时间 :
Copyright © 2022-Lei Qi | leiqi.top
搜索
数据加载中