栈
type Stack struct{
arr []int
used int
capcity int
}
func NewStack(capicity int) *Stack{
stack := &Stack{}
stack.arr = make([]int,capicity)
stack.used = 0
stack.capcity = capicity
return stack
}
func (this *Stack)push(item int)bool{
if this.capcity == this.used {
return false
}
this.arr[this.used] = item
this.used++
return true
}
func (this *Stack)pop() int{
if this.used == 0 {
return -1
}
tmp := this.arr[this.used-1]
this.used--
return tmp
}
type Node struct {
value int
next *Node
}
type Stack struct {
top *Node
}
func NewStack()*Stack{
stack := &Stack{}
newNode := &Node{}
stack.top = newNode
return stack
}
func (this *Stack)push(value int) bool{
newNode := &Node{}
newNode.value = value
newNode.next = this.top.next
this.top.next = newNode
return true
}
func (this *Stack)pop() int{
if this.top.next == nil {
return -1
}
tmp := this.top.next.value
this.top.next = this.top.next.next
return tmp
}
队列
type Queue struct {
arr []int
head int
tail int
capicity int
}
func NewQueue(capicity int)*Queue{
queue := &Queue{}
queue.arr = make([]int,capicity)
queue.head = 0
queue.tail = 0
queue.capicity = capicity
return queue
}
func (this *Queue)push(value int) bool{
if this.tail == this.capicity {
if(this.head == 0){
return false
}
for i:=this.head; i<this.tail; i++{
this.arr[i-this.head] = this.arr[i]
}
}
this.arr[this.tail] = value
this.tail++
return true
}
func (this *Queue)pop()int{
if this.head == this.tail{
return -1
}
value := this.arr[this.head]
this.head++
return value
}
func (this *LoopQueue) pop() int{
if this.head == this.tail {
return -1
}
value := this.arr[this.head]
this.head = (this.head+1) % this.count
return value
}
func (this *LoopQueue) push(value int) bool{
if (this.tail + 1) % this.count == this.head{
return false
}
this.arr[this.tail] = value
this.tail = (this.tail+1) % this.count
return true
}
func (this *Queue)push(value int)bool{
newNode := &Node{}
newNode.data = value
if this.head == nil {
this.head = newNode
this.tail = newNode
}else{
this.tail.next = newNode
this.tail = newNode
}
return true
}
func (this *Queue)pop()int{
if this.head == nil{
return -1
}
tmp := this.head.data
this.head = this.head.next
return tmp
}
递归
var note = map[int]int{}
func fib(n int) int {
if n == 1|| n == 0 {
return n
}
if value,ok := note[n];ok{
note[n] = value
return value
}
result := fib(n-1)+fib(n-2)
note[n] = result
return note[n]
}
func Factorial(n int)int{
if n == 1 {
return 1
}
return n * Factorial(n-1)
}
var note = map[int]int{}
func FactOptimize(n int) int {
if n == 1 {
return 1
}
if val,ok := note[n]; ok {
return val
}
result := n * Factorial(n-1)
note[n] = result
return result
}
func funArr(arr []int,n int,length int){
if n >= length-1 {
fmt.Println(arr)
return
}else{
for i:=n;i<length;i++{
arr[i],arr[n] = arr[n],arr[i]
funArr(arr,n+1,length)
arr[n],arr[i] = arr[i],arr[n]
}
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
· 从 Windows Forms 到微服务的经验教训