谁能给我解释一下这段代码的时间复杂度。谢谢
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;
}发布于 2017-01-19 17:12:55
我认为它应该是O(n^2),因为内部while的时间复杂度是n,而外部while的时间复杂度也是如此。
https://stackoverflow.com/questions/41737639
复制相似问题