栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

进行编程:生成组合

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

进行编程:生成组合

您的老师已经暗示您应该使用频道而不是返回大数组。通过这样解决,您将不必存储包含所有组合的大数据块,而只需为函数提供不同的迭代并一次处理它们即可。

我们可以看到您的老师提示,

GenerateCombinations
返回a
chan string
而不是a
[]string

func GenerateCombinations(alphabet string, length int) <-chan string

这也意味着该函数必须1)创建要返回的通道,以及2)启动将例程传递给该通道的goroutine。该函数如下所示:

func GenerateCombinations(alphabet string, length int) <-chan string {    c := make(chan string)    // Starting a separate goroutine that will create all the combinations,    // feeding them to the channel c    go func(c chan string) {        defer close(c) // once the iteration function is finished, we close the channel        // This is where the iteration will take place        // Your teacher's pseudo pre uses recursion        // which mean you might want to create a separate function        // that can call itself.    }(c)    return c // Return the channel to the calling function}

虽然我将实际迭代留给您,但每个循环都应导致您将组合字符串放入通道中。因为它不是缓冲通道,所以迭代函数将等待主函数读取该值,然后再继续处理下一个迭代。

包含主要功能的游乐场版本:http :
//play.golang.org/p/CBkSjpmQ0t

使用递归的完整的工作解决方案:http :
//play.golang.org/p/0bWDCibSUJ



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/448220.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号