登录
首页 >  Golang >  Go问答

如何在golang中创建静态局部变量

来源:stackoverflow

时间:2024-03-16 19:21:18 123浏览 收藏

在 Go 语言中,使用函数外部声明的映射会导致函数间共享该映射,从而造成错误的结果。同时,在函数内部声明映射会因递归调用而导致映射被不断重新初始化。因此,需要一种方法将映射声明为静态局部变量。 根据提供的文章,可以通过使用带有映射参数的不同函数来解决此问题。在最初调用 memo_cansum 函数时声明一个新映射,并使用新函数 momoWithMap 进行递归调用。这种方法允许每个递归调用使用自己的映射,从而实现静态局部变量的功能。

问题内容

/*
description: write a function cansum(targetsum, numbers) that takes in a
targetsum and an array of numbers as arguments.

the function should return a boolean indicating whether or not it is possible  
to generate the targetsum using numbers from the array

you may use an element of the slice as many times as needed.
assume that all input numbers are non-negative.

*/
package main

import "fmt"

func canSum(targetSum int, numbers ...int) bool {
    if targetSum == 0 {
        return true
    }
    if targetSum < 0 {
        return false
    }
    for index := 0; index < len(numbers); index++ {
        if canSum(targetSum-numbers[index], numbers...) == true {
            return true
        }
    }
    return false
}

//var memo = map[int]bool{} //Getting wrong values due to shared function scope from package scope
func memo_canSum(targetSum int, numbers ...int) bool {
    memo := map[int]bool{} //Defeats dp func since we continue to reinitialize during recursive calls
    if _, exists := memo[targetSum]; exists {
        return memo[targetSum]
    }

    if targetSum == 0 {
        return true
    }
    if targetSum < 0 {
        return false
    }
    for index := 0; index < len(numbers); index++ {
        if memo_canSum(targetSum-numbers[index], numbers...) == true {
            memo[targetSum] = true
            return memo[targetSum]
        }
    }
    memo[targetSum] = false
    return false
}

func main() {

    //Non-Dp Solution
    fmt.Println(canSum(21, 2, 4, 8))
    fmt.Println(canSum(80, 2, 4, 8))
    fmt.Println(canSum(300, 7, 14))

    //Dp Solution
    fmt.Println(memo_canSum(21, 2, 4, 8))
    fmt.Println(memo_canSum(80, 2, 4, 8))
    fmt.Println(memo_canSum(300, 7, 14))
}

所以我在使用 golang 编程时遇到了一个问题,如果我在函数之外声明我的记忆映射,那么所有函数调用都会共享相同的映射,并且会导致结果错误。 如果我在函数内声明映射,则每个递归调用都会重新初始化映射。有没有办法让地图成为静态局部变量?


正确答案


使用 map 参数 编写一个不同的函数并在其中进行递归。最初在 memo_cansum 函数中调用该函数并声明新地图。

//var memo = map[int]bool{} //Getting wrong values due to shared function scope from package scope
func memo_canSum(targetSum int, numbers ... int) bool {
    memo := map[int]bool{} //Defeats dp func since we continue to reinitialize during recursive calls
    return momoWithMap(memo, targetSum, numbers...)
}

func momoWithMap(memo map[int]bool, targetSum int, numbers ... int) bool{
    if _, exists:=memo[targetSum]; exists{
        return memo[targetSum]
    }

    if targetSum == 0 {
        return true
    }
    if targetSum < 0{
        return false
    }
    for index:= 0; index < len(numbers); index++{
        if momoWithMap(memo, targetSum - numbers[index], numbers...) == true {
            memo[targetSum] = true
            return true
        }
    }
    memo[targetSum] = false
    return false
}

终于介绍完啦!小伙伴们,这篇关于《如何在golang中创建静态局部变量》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>