谁能解释一下这段代码的时间复杂度。谢谢
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
的时间复杂度是相同的。