这个方法会创建额外的空间吗?

huangapple 未分类评论43阅读模式
英文:

Does this method creates extra space?

问题

{
    static Stack side = new Stack();
    static Object top = new Object();
    static Object bottom = new Object();
    static Stack st = new Stack();

    public static void main(String[] args) {
        st.push(1);
        st.push(2);
        st.push(3);
        st.push(4);
        st.push(5);
        st.push(6);
        st.push(7);
        st.push(8);
        st.push(9);
        reverse(st);
    }

    static void reverse(Stack st) {
        top = st.peek();
        st.pop();
        transfer(st, side, bottom);
        addToBottom(st, top);
        bottom = st.peek();
        transfer(side, st, bottom);
        if (st.peek() != top) reverse(st);
    }

    static void transfer(Stack st, Stack side, Object bottom) {
        while (!st.empty()) {
            if (st.peek() != bottom) {
                side.push(st.peek());
                st.pop();
            } else break;
        }
    }
    
    static void addToBottom(Stack st, Object top) {
        if (st.empty()) {
            st.push(top);
            return;
        }
        
        Object temp = st.peek();
        st.pop();
        addToBottom(st, top);
        st.push(temp);
    }
}

To reverse the stack without creating extra space, the code has been modified to use a recursive approach in the addToBottom function. This approach pushes the top element to the bottom of the stack by recursively popping the elements from the stack, adding the top element when the stack becomes empty, and then pushing the previously popped elements back onto the stack. This ensures the reversal without requiring additional space.

As for optimizing the code to run in O(n) time complexity, the recursive approach already achieves this. The addToBottom function processes each element of the stack exactly once, leading to a linear time complexity of O(n), where n is the number of elements in the stack.

英文:

I'm trying to reverse a stack without creating extra space.

This is what I came about:

{
                
    static Stack side = new Stack();
    static Object top = new Object();
    static Object bottom=new Object();
    static Stack st = new Stack();

    public static void main(String[] args) {
        st.push(1);
        st.push(2);
        st.push(3);
        st.push(4);
        st.push(5);
        st.push(6);
        st.push(7);
        st.push(8);
        st.push(9);
        reverse(st);
    }
    
  
    
    static void reverse(Stack st){
        top = st.peek();
        st.pop();
        transfer(st,side,bottom);
        addToButtom(st, top);
        bottom=st.peek();
        transfer(side,st,bottom);
        if (st.peek()!=top)reverse(st);
    }
    static void transfer(Stack st, Stack side, Object bottom){
        while (!st.empty()) {
            if (st.peek()!=bottom) {
                side.push(st.peek());
                st.pop();
            }else break;
        }
     }
    static void addToButtom(Stack st, Object top){st.push(top);}     
}

If this method creates extra space, how can I fix it?

Another question is, how can I modify so that the code run in O(n)?

答案1

得分: 0

你可以简单地创建Vector的子类,然后添加一个切换方法,用于决定pop方法是从前面还是后面取出……可以预期这将是O(1)的。

英文:

You could always just make your own subclass of Vector and add a toggle method that determines whether the pop method to retrieves from either the front or the back... Presumably that would be O(1).

huangapple
  • 本文由 发表于 2020年7月23日 11:07:00
  • 转载请务必保留本文链接:https://java.coder-hub.com/63046260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定