How to make a static local variable in golang

huangapple go评论107阅读模式
英文:

How to make a static local variable in golang

问题

描述:编写一个函数canSum(targetSum, numbers),该函数接受一个目标和一个数字数组作为参数。

该函数应返回一个布尔值,指示是否可以使用数组中的数字生成目标和。

您可以根据需要多次使用切片的元素。
假设所有输入数字都是非负的。

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编程时遇到了一个问题,如果我在函数外部声明我的记忆化映射,则所有函数调用共享同一个映射,这会导致结果出错。
如果我在函数内部声明映射,则每次递归调用都会重新初始化映射。有没有办法使映射成为静态局部变量?

英文:
/*
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 &quot;fmt&quot;
func canSum(targetSum int, numbers ...int) bool {
if targetSum == 0 {
return true
}
if targetSum &lt; 0 {
return false
}
for index := 0; index &lt; 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 &lt; 0 {
return false
}
for index := 0; index &lt; 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))
}

So I've ran into an issue while programming with golang, if i declare my memoized map outside of my function then all function calls share the same map and it leads to error's with the results.
If i declare the map inside the function then each recursive call reinitializes the map. Is there any way to make the map a static local variable?

答案1

得分: 1

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

func memo_canSum(targetSum int, numbers ...int) bool {
    memo := map[int]bool{} //由于来自包范围的共享函数作用域,导致获取错误的值
    return momoWithMap(memo, targetSum, numbers...)
}

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

    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
}
英文:

Write a different function with map parameter and do recursive in it. Initially call that function inside your memo_canSum function with declaring new map.

//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 &lt; 0{
return false
}
for index:= 0; index &lt; len(numbers); index++{
if momoWithMap(memo, targetSum - numbers[index], numbers...) == true {
memo[targetSum] = true
return true
}
}
memo[targetSum] = false
return false
}

huangapple
  • 本文由 发表于 2021年5月21日 04:40:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/67627633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定