Arrays 计算递归函数内接收的数据

Arrays 计算递归函数内接收的数据,arrays,swift,function,recursion,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 f

函数通过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)
函数必须以某种方式返回这些组合的数目

我将非常感谢你的帮助

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


由于递归函数,没有全局变量无法管理计数。因此,根据您想要的输出,您编写的方法是完美的。

由于递归函数,没有全局变量,您无法管理计数。因此,根据您想要的输出,您编写的方法是完美的。

您不需要全局变量。至少还有两种选择。您可以将
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))

您不需要全局变量。至少还有两种选择。您可以将
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))

你好我只能把这些组合拿出来。但我还需要计算它们。(AAA、ABC、ABB、AAB……等等)我需要得到他们所有人的号码。我补充了我是如何试图解决这个问题的。(很可能,这不是最好的方法,因为我需要在函数中执行)您好。我只能把这些组合拿出来。但我还需要计算它们。(AAA、ABC、ABB、AAB……等等)我需要得到他们所有人的号码。我补充了我是如何试图解决这个问题的。(很可能,这不是最好的方法,因为我需要在函数中执行)抱歉,这是错误的。不需要全局搜索。这当然是一个可能的解决方案,但不是唯一的。对不起,这是错误的。不需要全局搜索。这当然是一个可能的解决方案,但不是唯一的解决方案。我认为为每个迭代在运行时生成一个全局变量比生成这么多变量更好。当然,有无止境的方法可以实现我们想要的,但我认为在编码时记住内存和资源同样重要。@SharadPaghadal使用
inout
的版本只有一个
count
值。请记住,使用返回值并为每个递归调用添加单独的
count
的版本仅为每个递归调用添加8个字节(一个
Int
)。即使算法达到100层深度,堆栈上也只有800字节。与程序使用的其余内存相比,这算不了什么。避免过早优化。首先编写更干净、更有条理的代码。然后,如果需要,研究优化。使用全局变量有其自身的潜在问题需要担心。@SharadPaghadal如果内存使用是一个问题(可能不是),那么您应该从减少
数据的CoW拷贝开始优化。我认为在每次迭代的运行时生成一个全局变量比生成这么多变量更好。当然,有无止境的方法可以实现我们想要的,但我认为在编码时记住内存和资源同样重要。@SharadPaghadal使用
inout
的版本只有一个
count
值。请记住,使用返回值并为每个递归调用添加单独的
count
的版本仅为每个递归调用添加8个字节(一个
Int
)。即使算法达到100层深度,堆栈上也只有800字节。与程序使用的其余内存相比,这算不了什么。避免过早优化。首先编写更干净、更有条理的代码。然后,如果需要,研究优化。使用全局变量也有其自身的潜在问题需要担心。@SharadPaghadal如果内存使用是一个问题(可能不是),那么您应该从减少
数据的CoW拷贝开始优化。