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