swift 回溯在编程语言中是如何工作的?

qmelpv7a  于 2022-11-21  发布在  Swift
关注(0)|答案(1)|浏览(115)

我正在使用递归回溯,我已经使用了多个递归调用
我的代码片段:-

func test(currentIndex: Int, op: inout [Int]) {
    if currentIndex > 1 {
        print("exit here**********************")
        return
    }
    print("##before", currentIndex)
    test(currentIndex: currentIndex+1, op: &op)
    print("after------------------------------------------------------", currentIndex)
    test(currentIndex: currentIndex+1, op: &op)
    print("^^^Final ================  ", currentIndex)
}

var op1: [Int] = []
test(currentIndex: 0, op: &op1)

打印-〉

##before 0
##before 1
exit here**********************
after------------------------------------------------------ 1
exit here**********************
^^^Final ================   1
after------------------------------------------------------ 0
##before 1
exit here**********************
after------------------------------------------------------ 1
exit here**********************
^^^Final ================   1
^^^Final ================   0

我无法理解最后4个打印语句。

h9a6wy2h

h9a6wy2h1#

您拥有:

func test(currentIndex: Int, op: inout [Int]) {
    if currentIndex > 1 {
        print("exit here**********************")
        return
    }
    print("##before", currentIndex)
    test(currentIndex: currentIndex+1, op: &op) //First
    print("after------------------------------------------------------", currentIndex)
    test(currentIndex: currentIndex+1, op: &op) //Second
    print("^^^Final ================  ", currentIndex)
}

var op1: [Int] = []
test(currentIndex: 0, op: &op1)

让我们手动分析启动测试的最后一行是什么。如果我们手动编写所有行,我们将在调试时复制/粘贴代码。我使用缩进来保持清晰。这是在“跟踪”代码。

//Index is 0:
if currentIndex > 1 {} //Skipped

print("##before", currentIndex)
test(currentIndex: currentIndex+1, op: &op) //First

    //Index is 1
    if currentIndex > 1 {} //Skipped

    print("##before", currentIndex)
    test(currentIndex: currentIndex+1, op: &op) //First

        //Index is 2
        if currentIndex > 1 {
            print("exit here**********************")
            return
        }

    print("after------------------------------------------------------", currentIndex)
    test(currentIndex: currentIndex+1, op: &op)  //Second

        //Index is 2
        if currentIndex > 1 {
            print("exit here**********************")
            return
        }

    print("^^^Final ================  ", currentIndex)

print("after------------------------------------------------------", currentIndex)
test(currentIndex: currentIndex+1, op: &op) //Second
    //Index is 1
    if currentIndex > 1 {} //Skipped

    print("##before", currentIndex)
    test(currentIndex: currentIndex+1, op: &op) //First

        //Index is 2
        if currentIndex > 1 {
            print("exit here**********************")
            return
        }

    print("after------------------------------------------------------", currentIndex)
    test(currentIndex: currentIndex+1, op: &op)  //Second

        //Index is 2
        if currentIndex > 1 {
            print("exit here**********************")
            return
        }

    print("^^^Final ================  ", currentIndex)

print("^^^Final ================  ", currentIndex)

因此,您可以获得:

##before 0 
##before 1 
exit here**********************
after------------------------------------------------------ 1
exit here********************** 
^^^Final ================   1
after------------------------------------------------------ 0
##before 1
exit here**********************
after------------------------------------------------------ 1
exit here**********************
^^^Final ================   1
^^^Final ================   0

您还可以使用调试器、放置断点、查看调用堆栈...

相关问题