簡體   English   中英

當我達到某個條件並簡單地將 True 返回到整個函數時,是否可以刪除/忽略遞歸/調用堆棧上的所有項目?

[英]Is it possible to delete/ignore all items on recursion/call stack, when I have hit a certain condition and simply return True to the overall function?

我正在回答一個問題,其中給出了 3 個字符串: onetwothree 我們想返回一個布爾值來說明我們是否可以通過交織字符串onetwo來創建string three

例如:

one = "aab", two = "aac", three = "aaab" -> this should return True 

然而

one = "abb", two = "acc", three = "aaab" -> this should return False

我的方法是兩個簡單地創建three pointers指向三個字符串開頭的three pointers 當我們在字符串one and threetwo and three之間找到匹配時,將它們一一遞增。

pointer_onepointer_two指向onetwo的同一個字符時,上面的內容就變得棘手了! 在這種情況下,我們需要運行recursion 現在我的問題是我們何時開始向遞歸recursive stack添加遞歸調用。 我們可能會到達string three的末尾,並且可以為整個函數return True

然而,在我們的遞歸堆棧中仍然有(部分完成的)遞歸調用! 所以這個最新的return True ,將被傳遞回前一個調用者,但我只想為整個函數return True並忽略遞歸堆棧中的剩余項目 - 無論如何要這樣做嗎?

或者我是否必須以這樣的方式編寫代碼,即在堆棧為空之前不return任何內容,然后return True到最終調用?

我寫的代碼很長,所以我暫時省略了它,但如果需要,我可以包含它。

謝謝!

我的代碼:

def interweavingStrings(one, two, three, pointer_one=0, pointer_two=0, pointer_three=0):
    possible_interwoven = False

    while pointer_three < len(three):

        if one[pointer_one] == two[pointer_two]:
            possible_interwoven = interweavingStrings(one, two, three, pointer_one + 1, pointer_two, pointer_three + 1)

            if not possible_interwoven:
                possible_interwoven = interweavingStrings(one, two, three, pointer_one, pointer_two + 1,
                                                          pointer_three + 1)

            if not possible_interwoven:
                break

        if pointer_two <= len(two) - 1 and three[pointer_three] == two[pointer_two]:
            pointer_two += 1
            pointer_three += 1
            possible_interwoven = True

        if pointer_one <= len(one) - 1 and three[pointer_three] == one[pointer_one]:
            pointer_one += 1
            pointer_three += 1
            possible_interwoven = True

        if pointer_two <= len(two) - 1 and pointer_one <= len(one) - 1 and one[pointer_one] != three[pointer_three] and two[pointer_two] != two[pointer_two]:
            return False

    return possible_interwoven

這應該是遞歸函數結構的一部分。 當您進行遞歸調用時,如果它返回 True,則從當前調用返回一個值為 True。

例如:

def canweave(a,b,c):
    if not c : return True
    if len(a)+len(b)<len(c): return False 
    if a and a[0]==c[0] and canweave(a[1:],b,c[1:]):
        return True                                   # works with 1st of a
    if b and b[0]==c[0] and canweave(a,b[1:],c[1:]):
        return True                                   # works with 1st of b
    return canweave(a[1:],b[1:],c)    # try with other chars
                                      # return result from recursion

print(canweave("aab","aac","aaab")) # True
print(canweave("abb","acc","aaab")) # False

對您的代碼的觀察

possible_interwoven = interweavingStrings(...)應該是確定的,而不僅僅是一種可能性。 當您從該調用中獲得 True 時,您必須確定其余字符是“可交織的”。 因此,當possible_interwoven為 True 時,您應該立即返回 True。 這將自動滴入遞歸調用以產生最終結果。

您如何推進指針也存在問題,但我無法通過簡單的調整找到一種簡單的方法來解決這個問題。

這是使用您的指針方法的修訂版本:

def interweavingStrings(one, two, three, 
                        pointer_one=0, pointer_two=0, pointer_three=0):
    if pointer_three == len(three): 
        return True
    if pointer_one >= len(one) and pointer_two >= len(two): 
        return False
    if pointer_one < len(one) and one[pointer_one] == three[pointer_three]:
        if interweavingStrings(one,two,three,
                               pointer_one+1,pointer_two,pointer_three+1):
            return True
    if pointer_two < len(two) and two[pointer_two] == three[pointer_three]:
        if interweavingStrings(one,two,three, 
                               pointer_one,pointer_two+1,pointer_three+1):
            return True
    return interweavingStrings(one,two,three, 
                               pointer_one+1,pointer_two+1,pointer_three)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM