go
hust_joker
这个作者很懒,什么都没留下…
展开
-
go的sort包源码分析与排序算法解析
1、sort包的使用 Sort函数的源码:func Sort(data Interface) { n := data.Len() quickSort(data, 0, n, maxDepth(n))} 再看Interface的类型,是一个包含了三个函数的接口类型:type Interface interface { // Len is the number of elements in the collection. Len() int Less(i, j int) bool原创 2021-11-08 15:48:02 · 334 阅读 · 0 评论 -
二叉树填充每个节点的下一个右侧节点指针
/** * Definition for a Node. * type Node struct { * Val int * Left *Node * Right *Node * Next *Node * } */type CircleQueue struct { Buff [4096]*Node Cap int Length int Start int End int}func (p *CircleQueue)原创 2021-07-13 10:10:11 · 114 阅读 · 0 评论 -
从前序和中序遍历构建二叉树
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func caller(preorder []int, inorder []int, indexMap map[int]int,inOrderStart, inOrderEnd, preOrderStart, preOrderEnd in原创 2021-07-08 20:47:11 · 83 阅读 · 0 评论 -
从中序与后序遍历序列构造二叉树递归做法
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func caller(inorder []int, postorder []int, indexMap map[int]int, inOrderStart, inOrderEnd, postOrderStart, postOrderE原创 2021-07-08 17:08:43 · 127 阅读 · 0 评论 -
leetcode面试题二叉树路径总和迭代实现
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */type StackNode struct { Parr [1000]*TreeNode Index int}func(s *StackNode) IsEmpty() bool { if s.Index &l原创 2021-06-01 17:12:42 · 112 阅读 · 0 评论 -
leetcode二叉树路径总和递归实现
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func hasPathSum(root *TreeNode, targetSum int) bool { if nil == root { return false } if root.Left原创 2021-06-01 14:45:44 · 72 阅读 · 0 评论 -
循环队列leetcode面试解答
type MyCircularQueue struct { Parr []int Head int Tail int Length int Cap int}func Constructor(k int) MyCircularQueue { return MyCircularQueue{ Parr: make([]int, k), Head: 0, Tail: 0, Length: k,原创 2021-05-28 09:22:27 · 87 阅读 · 0 评论 -
二叉树后序遍历迭代实现
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } *//** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right原创 2021-05-27 14:44:28 · 166 阅读 · 0 评论 -
二叉树后序遍历递归实现
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func postorderTraversal(root *TreeNode) []int { rsp := make([]int, 0) if nil == root { return rsp }原创 2021-05-27 09:45:10 · 86 阅读 · 0 评论 -
二叉树中序遍历迭代实现
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */type Stack struct { Parr [1000]*TreeNode Index int}func(s *Stack) IsEmpty() bool { if s.Index <= 0 {原创 2021-05-27 09:26:44 · 78 阅读 · 0 评论 -
二叉树中序遍历递归实现
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func inorderTraversal(root *TreeNode) []int { rsp := make([]int, 0) if nil == root { return rsp }原创 2021-05-26 17:27:26 · 160 阅读 · 0 评论 -
二叉树前序遍历迭代实现
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */type Stack struct { Parr [1000]*TreeNode Index int}func(s *Stack) IsEmpty() bool { if s.Index <= 0 {原创 2021-05-26 16:55:21 · 107 阅读 · 0 评论 -
二叉树前序递归实现
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func preorderTraversal(root *TreeNode) []int { rsp := make([]int, 0) if nil == root { return rsp原创 2021-05-26 16:28:19 · 83 阅读 · 0 评论 -
golang的interface与反射实现原理
1、什么是interface interface通过定义一系列的方法集合指定了其他类型的行为。对于一个类型,当且仅当它实现了某一个interface方法集的所有方法,我们称这个类型满足了这个interface。可以理解为interface是一个抽象的类型,这个类型定义了一系列的方法,当某个其他类型完全实现了该interface的所有的方法,可以称这个类型是当前抽象interface的一个实例。可以把interface理解为一个合约,方法即是合同细节,当其他类型满足了所有细节(方法),即可认为该类型满足了原创 2021-05-06 16:08:25 · 1697 阅读 · 3 评论 -
golang内存分配与管理
概述 golang的内存分配机制源自Google的tcmalloc算法,英文全称thread caching malloc,从名字可以看出,是在原有基础上,针对多核多线程的内存管理进行优化而提出来的。该算法的核心思想是内存的多级管理,进而降低锁的粒度;将内存按需划成大小不一的块,减少内存的碎片化。为每个P,也就是go协程调度模型了里面的逻辑处理器维护一个mcache结构体的独立内存池,只有当该内存池不足时,才会向全局mcentral和mheap结构体管理的内存池申请。为每一个P维持一个私有的本地内存池,原创 2021-03-12 16:14:07 · 1946 阅读 · 3 评论 -
gin框架源码浅析
最近项目中的几个模块用到了Gin框架,记录一下看源码的心得。 1、Gin框架简介 Gin框架是一个go开源的轻量级http服务器框架 官方文档:https://gin-gonic.com/docs/ 源码地址:https://github.com/gin-gonic/gin 中文教程:https://learnku.com/docs/gin-gonic/2019 主要有以下功能特性: a、基于redixtree的路由策略,没有使用反射,占用内存小。采用.原创 2021-03-02 14:44:47 · 872 阅读 · 0 评论