arrays - 计算递归函数内部收到的数据

标签 arrays swift function recursion

该函数通过 print() 输出字符“abc”的所有可能组合。 (视指定长度而定) 我需要计算这个数额。我只是设法通过 print() 将这些组合一一输出。我在代码的正确位置留下了评论。

func allLexicographicRecur (_ string: [String.Element], _ data: [String], _ last: Int, _ index: Int){
    var length = string.count-1
    var data = data
    for i in 0...length {
        data[index] = String(string[i])
        if index == last {
            print(data.joined()) // Displays a combination. It is necessary to somehow calculate.
        }else{
            allLexicographicRecur(string, data, last, index+1)
        }

    }
}


func allLexicographic(_ l: Int) {
    var alphabet = "abc"
    var data = Array(repeating: "", count: l)
    var string = alphabet.sorted()
    var counter = 0
    allLexicographicRecur(string, data, l-1, 0)
}


allLexicographic(3)

函数必须以某种方式返回这些组合的数量。

非常感谢您的帮助!

我设法只用这种方式计数(但很可能这不是最好的方式):

var count = 0
func allLexicographicRecur (_ string: [String.Element], _ data: [String], _ last: Int, _ index: Int){
    var length = string.count-1
    var data = data
    for i in 0...length {
        data[index] = String(string[i])
        if index == last {
            print(data.joined()) // Displays a combination. It is necessary to somehow calculate.
            count += 1
        }else{
            allLexicographicRecur(string, data, last, index+1)
        }

    }
}


func allLexicographic(_ l: Int) {
    var alphabet = "abc"
    var data = Array(repeating: "", count: l)
    var string = alphabet.sorted()
    var counter = 0
    allLexicographicRecur(string, data, l-1, 0)
}


allLexicographic(3)
print(count)

最佳答案

您不需要全局变量。至少还有两个其他选择。您可以将 inout 参数添加到 allLexicographicRecur 以跟踪计数,或者您可以让 allLexicographicRecur 返回其计数。

这是使用返回值的代码:

func allLexicographicRecur(_ string: [String.Element], _ data: [String], _ last: Int, _ index: Int) -> Int {
    let length = string.count - 1
    var data = data
    var count = 0
    for i in 0...length {
        data[index] = String(string[i])
        if index == last {
            print(data.joined()) // Displays a combination. It is necessary to somehow calculate.
            count += 1
        } else {
            count += allLexicographicRecur(string, data, last, index + 1)
        }
    }

    return count
}

func allLexicographic(_ l: Int) -> Int {
    let alphabet = "abc"
    let data = Array(repeating: "", count: l)
    let string = alphabet.sorted()
    return allLexicographicRecur(string, data, l - 1, 0)
}

print(allLexicographic(3))

您的代码已更新为使用 inout 参数。

func allLexicographicRecur(_ string: [String.Element], _ data: [String], _ last: Int, _ index: Int, _ count: inout Int){
    let length = string.count - 1
    var data = data
    for i in 0...length {
        data[index] = String(string[i])
        if index == last {
            print(data.joined()) // Displays a combination. It is necessary to somehow calculate.
            count += 1
        } else {
            allLexicographicRecur(string, data, last, index + 1, &count)
        }
    }
}

func allLexicographic(_ l: Int) -> Int {
    let alphabet = "abc"
    let data = Array(repeating: "", count: l)
    let string = alphabet.sorted()
    var counter = 0
    allLexicographicRecur(string, data, l - 1, 0, &counter)
    return counter
}

print(allLexicographic(3))

关于arrays - 计算递归函数内部收到的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57313388/

相关文章:

javascript - 如何将对象转换为排序数组?

php - mysql_fetch_array 仅获取第一个结果,即使使用 while 循环

ios - 遍历两个自定义数组并在变量相等时设置值 Swift

java - 无法在 Java 中存储对象数组长度

iOS Objective-C 在 VoiceOver 处于事件状态时关闭接近监控

swift - 确定 View 是否到达 UIScrollView 的顶部

swift - switch 语句中 case 的顺序重要吗?

java - 如何在私有(private)类中调用与另一个函数名称相同的函数?

java - 是否有任何好的函数库可用于 Java 中的集合,例如

c++ - 来自传递参数的函数的默认值