[英]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.