Java 用两个堆栈实现优先级堆栈 这是我认为它可能是要工作的:你有1个堆栈,它是根据优先级排序的,每次当一个新元素出现在栈的中间时,你将栈1中的所有元素弹出到堆栈2,将元素添加到适当的位置,然后从堆栈2弹出所有元素回到栈1。
有人知道更有效的解决方案吗?您不需要在内部使用堆栈,因为您将堆栈作为外部接口(封装!)。只要插入到您正在使用的任何集合的中间。您不需要在内部使用堆栈,因为您将堆栈作为外部接口(封装!)。只要插入到您正在使用的任何集合的中间。我唯一能想到的提高效率的方法是在插入后将项目分割在两个堆栈之间Java 用两个堆栈实现优先级堆栈 这是我认为它可能是要工作的:你有1个堆栈,它是根据优先级排序的,每次当一个新元素出现在栈的中间时,你将栈1中的所有元素弹出到堆栈2,将元素添加到适当的位置,然后从堆栈2弹出所有元素回到栈1。,java,Java,有人知道更有效的解决方案吗?您不需要在内部使用堆栈,因为您将堆栈作为外部接口(封装!)。只要插入到您正在使用的任何集合的中间。您不需要在内部使用堆栈,因为您将堆栈作为外部接口(封装!)。只要插入到您正在使用的任何集合的中间。我唯一能想到的提高效率的方法是在插入后将项目分割在两个堆栈之间 如果下一个操作是insert(而不是插入新的最高优先级项),则在上一次插入后将项从堆栈2移回堆栈1只需将其移回堆栈2即可进行下一次插入 如果下一个操作是pop或插入一个新的最高优先级项,那么您可以将这些项从堆栈2
public void insert(int value) {
// slide higher priority items on lower stack to higher stack temporarily
while (! lowerStack.isEmpty() && lowerStack.peek() > value) {
higherStack.push(lowerStack.pop());
}
assert lowerStack.isEmpty() || lowerStack.peek() <= value;
assert higherStack.isEmpty() || higherStack.peek() >= value);
lowerStack.push(value);
// slide them back
while (! higherStack.isEmpty()) {
lowerStack.push(higherStack.pop());
}
assert higherStack.isEmpty();
}
public int pop() throws NoSuchElementException {
assert higherStack.isEmpty();
return lowerStack.pop(); // will throw if empty
}
public void insert(int值){
//将较低堆栈上的较高优先级项目临时滑动到较高堆栈
而(!lowerStack.isEmpty()&&lowerStack.peek()>value){
较高的钉住。推(较低的钉住。弹出());
}
断言lowerStack.isEmpty()| | lowerStack.peek()=值);
低粘性推力(值);
//把它们滑回去
而(!higherStack.isEmpty()){
较低的钉住。推(较高的钉住。弹出());
}
断言higherStack.isEmpty();
}
public int pop()抛出NoTouchElementException{
断言higherStack.isEmpty();
返回lowerStack.pop();//如果为空,将抛出
}
备选方案:
public void insert(int value) {
// only one of the while loops below should really execute
// slide any higher priority items on lower stack to higher stack
while (! lowerStack.isEmpty() && lowerStack.peek() > value) {
higherStack.push(lowerStack.pop());
}
// slide any lower priority items on higher stack to lower stack
while (! higherStack.isEmpty() && higherStack.peek() < value) {
lowerStack.push(higherStack.pop());
}
assert lowerStack.isEmpty() || lowerStack.peek() <= value;
assert higherStack.isEmpty() || higherStack.peek() >= value);
lowerStack.push(value);
}
public T pop() throws NoSuchElementException {
// get to highest priority item
while (! higherStack.isEmpty()) {
lowerStack.push(higherStack.pop());
}
assert higherStack.isEmpty();
return lowerStack.pop(); // will throw if empty
}
public void insert(int值){
//下面的while循环中只有一个应该真正执行
//将较低堆栈上的任何较高优先级项目滑动到较高堆栈
而(!lowerStack.isEmpty()&&lowerStack.peek()>value){
较高的钉住。推(较低的钉住。弹出());
}
//将较高堆栈上的任何较低优先级项目滑动到较低堆栈
而(!higherStack.isEmpty()&&higherStack.peek()
我唯一能想到的提高效率的方法是在插入后将项目分成两个堆栈
public void insert(int value) {
// slide higher priority items on lower stack to higher stack temporarily
while (! lowerStack.isEmpty() && lowerStack.peek() > value) {
higherStack.push(lowerStack.pop());
}
assert lowerStack.isEmpty() || lowerStack.peek() <= value;
assert higherStack.isEmpty() || higherStack.peek() >= value);
lowerStack.push(value);
// slide them back
while (! higherStack.isEmpty()) {
lowerStack.push(higherStack.pop());
}
assert higherStack.isEmpty();
}
public int pop() throws NoSuchElementException {
assert higherStack.isEmpty();
return lowerStack.pop(); // will throw if empty
}
public void insert(int值){
//将较低堆栈上的较高优先级项目临时滑动到较高堆栈
而(!lowerStack.isEmpty()&&lowerStack.peek()>value){
较高的钉住。推(较低的钉住。弹出());
}
断言lowerStack.isEmpty()| | lowerStack.peek()=值);
低粘性推力(值);
//把它们滑回去
而(!higherStack.isEmpty()){
较低的钉住。推(较高的钉住。弹出());
}
断言higherStack.isEmpty();
}
public int pop()抛出NoTouchElementException{
断言higherStack.isEmpty();
返回lowerStack.pop();//如果为空,将抛出
}
备选方案:
public void insert(int value) {
// only one of the while loops below should really execute
// slide any higher priority items on lower stack to higher stack
while (! lowerStack.isEmpty() && lowerStack.peek() > value) {
higherStack.push(lowerStack.pop());
}
// slide any lower priority items on higher stack to lower stack
while (! higherStack.isEmpty() && higherStack.peek() < value) {
lowerStack.push(higherStack.pop());
}
assert lowerStack.isEmpty() || lowerStack.peek() <= value;
assert higherStack.isEmpty() || higherStack.peek() >= value);
lowerStack.push(value);
}
public T pop() throws NoSuchElementException {
// get to highest priority item
while (! higherStack.isEmpty()) {
lowerStack.push(higherStack.pop());
}
assert higherStack.isEmpty();
return lowerStack.pop(); // will throw if empty
}
public void insert(int值){
//下面的while循环中只有一个应该真正执行
//将较低堆栈上的任何较高优先级项目滑动到较高堆栈
而(!lowerStack.isEmpty()&&lowerStack.peek()>value){
较高的钉住。推(较低的钉住。弹出());
}
//将较高堆栈上的任何较低优先级项目滑动到较低堆栈
而(!higherStack.isEmpty()&&higherStack.peek()
如果是面试问题,您可能需要回答“堆栈根本没有优先级。它是优先级队列。”:)如果是面试问题,您可能需要回答“堆栈根本没有优先级。它是优先级队列。”:)如果一个堆栈保持奇数优先级项的顺序,而另一个堆栈保持偶数优先级项的顺序,该怎么办
- 在插入时,检查要插入的堆栈。您仍然可以使用另一个堆栈的顶部暂时保存项目
- 在pop上,比较哪个堆栈的优先级较高,然后弹出那个堆栈