簡體   English   中英

該算法的時間復雜度如何?為什么?

[英]what would be the time complexity of this algorithm and why?

誰能解釋一下這段代碼的時間復雜度。 謝謝

public static Stack<Integer> sortStack(Stack<Integer> aStack) {

    Stack<Integer> rStack=new Stack<>();
    int temp=0;

    rStack.push(aStack.pop());

    while(!aStack.empty()){
        temp=aStack.pop();

        while(!rStack.empty() && temp >rStack.peek()){
            aStack.push(rStack.pop());
        }
        rStack.push(temp);
    }
    return rStack;
}

我認為這將是O(n ^ 2),因為內部while的時間復雜度是n, while外部while的時間復雜度是相同的。

暫無
暫無

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

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