在Go语言中,使用整数切片进行子集检查。

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

Subset check with integer slices in Go

问题

我正在寻找一种高效的方法来检查一个切片是否是另一个切片的子集。我可以简单地迭代它们来进行检查,但我觉得应该有更好的方法。

例如:

{1, 2, 3} 是 {1, 2, 3, 4} 的子集
{1, 2, 2} 不是 {1, 2, 3, 4} 的子集

如何以高效的方式进行这种检查呢?

谢谢!

英文:

I am looking for a efficient way to check if a slice is a subset of another. I could simply iterate over them to check, but I feel there has to be a better way.

E.g.

> {1, 2, 3} is a subset of {1, 2, 3, 4}
> {1, 2, 2} is NOT a subset of {1, 2, 3, 4}

What is the best way to do this efficiently?

Thanks!

答案1

得分: 6

我认为解决子集问题最常见的方法是使用映射。

  1. package main
  2. import "fmt"
  3. // subset函数返回true,如果第一个数组完全包含在第二个数组中。
  4. // 第二个数组中必须至少有与第一个数组中相同数量的重复值。
  5. func subset(first, second []int) bool {
  6. set := make(map[int]int)
  7. for _, value := range second {
  8. set[value] += 1
  9. }
  10. for _, value := range first {
  11. if count, found := set[value]; !found {
  12. return false
  13. } else if count < 1 {
  14. return false
  15. } else {
  16. set[value] = count - 1
  17. }
  18. }
  19. return true
  20. }
  21. func main() {
  22. fmt.Println(subset([]int{1, 2, 3}, []int{1, 2, 3, 4}))
  23. fmt.Println(subset([]int{1, 2, 2}, []int{1, 2, 3, 4}))
  24. }

检查重复值的能力相对较少见。上面的代码解决了所提出的问题(参见:http://play.golang.org/p/4_7Oh-fgDQ)。如果你计划有重复值,你将不得不像上面的代码那样保持一个计数。如果不会有重复值,你可以更简洁地通过使用布尔值作为映射值来解决问题。

英文:

I think the most common way to solve a subset problem is via a map.

  1. package main
  2. import &quot;fmt&quot;
  3. // subset returns true if the first array is completely
  4. // contained in the second array. There must be at least
  5. // the same number of duplicate values in second as there
  6. // are in first.
  7. func subset(first, second []int) bool {
  8. set := make(map[int]int)
  9. for _, value := range second {
  10. set[value] += 1
  11. }
  12. for _, value := range first {
  13. if count, found := set[value]; !found {
  14. return false
  15. } else if count &lt; 1 {
  16. return false
  17. } else {
  18. set[value] = count - 1
  19. }
  20. }
  21. return true
  22. }
  23. func main() {
  24. fmt.Println(subset([]int{1, 2, 3}, []int{1, 2, 3, 4}))
  25. fmt.Println(subset([]int{1, 2, 2}, []int{1, 2, 3, 4}))
  26. }

The ability to check duplicate values is relatively uncommon. The code above solves the problem as asked (see: http://play.golang.org/p/4_7Oh-fgDQ) though. If you plan on having duplicate values, you'll have to keep a count like the code above does. If there will not be duplicate values, you can solve the problem more compactly by using a boolean for the map value instead of an integer.

答案2

得分: 0

如果你的切片已经排序好,这段代码可以完成任务。

  1. package main
  2. import "fmt"
  3. // Subset 判断a是否是b的子列表。a和b都必须是(弱)升序的。
  4. func Subset(a, b []int) bool {
  5. for len(a) > 0 {
  6. switch {
  7. case len(b) == 0:
  8. return false
  9. case a[0] == b[0]:
  10. a = a[1:]
  11. b = b[1:]
  12. case a[0] < b[0]:
  13. return false
  14. case a[0] > b[0]:
  15. b = b[1:]
  16. }
  17. }
  18. return true
  19. }
  20. func main() {
  21. cases := []struct {
  22. a, b []int
  23. want bool
  24. }{
  25. {[]int{1, 2, 3}, []int{1, 2, 3, 4}, true},
  26. {[]int{1, 2, 2}, []int{1, 2, 3, 4}, false},
  27. }
  28. for _, c := range cases {
  29. if Subset(c.a, c.b) != c.want {
  30. fmt.Printf("Subset(%v, %v) = %v, want %v\n", c.a, c.b, Subset(c.a, c.b), c.want)
  31. }
  32. }
  33. }

这段代码定义了一个函数 Subset,用于判断切片 a 是否是切片 b 的子列表。切片 ab 都必须是(弱)升序的。在 main 函数中,定义了一些测试用例,并通过调用 Subset 函数进行测试。如果测试结果与期望结果不一致,则输出错误信息。

英文:

If your slices are sorted, this does the job.

  1. package main
  2. import &quot;fmt&quot;
  3. // Subset return whether a is a sublist of b. Both a and b must be (weakly) ascending.
  4. func Subset(a, b []int) bool {
  5. for len(a) &gt; 0 {
  6. switch {
  7. case len(b) == 0:
  8. return false
  9. case a[0] == b[0]:
  10. a = a[1:]
  11. b = b[1:]
  12. case a[0] &lt; b[0]:
  13. return false
  14. case a[0] &gt; b[0]:
  15. b = b[1:]
  16. }
  17. }
  18. return true
  19. }
  20. func main() {
  21. cases := []struct {
  22. a, b []int
  23. want bool
  24. }{
  25. {[]int{1, 2, 3}, []int{1, 2, 3, 4}, true},
  26. {[]int{1, 2, 2}, []int{1, 2, 3, 4}, false},
  27. }
  28. for _, c := range cases {
  29. if Subset(c.a, c.b) != c.want {
  30. fmt.Printf(&quot;Subset(%v, %v) = %v, want %v\n&quot;, c.a, c.b, Subset(c.a, c.b), c.want)
  31. }
  32. }
  33. }

答案3

得分: -1

检查一个数组是否是另一个数组的子集的算法(使用golang)

  1. package main
  2. import "fmt"
  3. func compare(arr1 []int, arr2 []int) bool {
  4. for _, num1 := range arr2 {
  5. status := false
  6. for _, num2 := range arr1 {
  7. if num1 == num2 {
  8. status = true
  9. break
  10. }
  11. }
  12. if !status {
  13. return false
  14. }
  15. }
  16. return true
  17. }
  18. func main() {
  19. arr1 := []int{1, 2, 3, 4, 5}
  20. arr2 := []int{1, 2, 3}
  21. if compare(arr1, arr2) {
  22. fmt.Println("arr2是arr1的子集")
  23. } else {
  24. fmt.Println("arr2不是arr1的子集")
  25. }
  26. }

这段代码是用来检查一个数组是否是另一个数组的子集的。

英文:

Algorithm to check if an array is a subset of another array in golang

  1. package main
  2. import &quot;fmt&quot;
  3. func compare(arr1 []int, arr2 []int) bool {
  4. for _, num1 := range arr2 {
  5. status := false
  6. for _, num2 := range arr1 {
  7. if num1 == num2 {
  8. status = true
  9. break
  10. }
  11. }
  12. if !status {
  13. return false
  14. }
  15. }
  16. return true
  17. }
  18. func main() {
  19. arr1 := []int{1, 2, 3, 4, 5}
  20. arr2 := []int{1, 2, 3}
  21. if compare(arr1, arr2) {
  22. fmt.Println(&quot;arr2 is subset of arr1&quot;)
  23. } else {
  24. fmt.Println(&quot;arr2 is not subset of arr1&quot;)
  25. }
  26. }

huangapple
  • 本文由 发表于 2013年9月19日 01:57:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/18879109.html
匿名

发表评论

匿名网友

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

确定