1.实现一个含有特殊功能的栈结构:在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作getMin>
   要求:
  1)pop/push/getMin 操作的事件复杂度都为O(1)
  2)设计的栈类型可以使用现成的栈结构

public class SolutionOne {

    private LinkedList<Integer> stackData = new LinkedList<>();
    private LinkedList<Integer> stackMin = new LinkedList<>();
    
    public void push(int node){
         
         stackData.push(node);
         //当被压如栈的值比stackMin的栈顶元素小时,在将当前入栈元素压如stackMin栈,否则,
         //向stackMin压如stackMin当前栈顶元素(即最小元素)
         if(stackMin.size() == 0 || node < stackMin.peek()){
            //向stackMin压如当前值
             stackMin.push(node);
         }else{
            //否则,向stackMin压如自身栈顶元素
             stackMin.push(stackMin.peek());
         }
    }
    
    //弹栈时,两栈同时弹出
    public int pop(){
        stackMin.pop();
        return stackData.pop();
    }
    
    //返回栈顶元素
    public int top(){
        return stackData.peek();
    }
    
    //获取栈中最小元素
    public int min(){
        return stackMin.peek();
    }
}

2.编写一个类,只能用两个栈结构实现队列,支持队列的基本操作(push,pop)。
    给定一个操作序列ope及它的长度n,其中元素为正数代表push操作,为0代表pop操作,保证操作序列合法且一定含pop操作,请返回pop的结果序列。
    测试样例:
    [1,2,3,0,4,0],6
    返回:[1,2]

public class SolutionTwo {

    public static void main(String[] args) {
        //[1,2,3,0,4,0],6
        
        int[] ope = {1,2,3,0,4,0};
        int[] result = twoStack(ope, 6);
        //Arrays.asList(ope).forEach(action);
        for(int i = 0;i < result.length;i ++){
            System.out.println(result[i]);
        }
    }
    
    public static int[] twoStack(int[] ope, int n){
        
        Stack<Integer> stack1 = new Stack<Integer>();
        Stack<Integer> stack2 = new Stack<Integer>();
        
        //
        int count = 0;   //记录弹栈的次数
        int[] result;
        
        //遍历给定的数组
        for(int i = 0;i < n;i ++){
            if(ope[i] != 0){  //向stack1中压栈
                stack1.push(ope[i]);
            }else{
                count ++;
            }
        }//压栈完成
        result = new int[count];
        
        
        //将stack1中的数据导入stack2
        while(!stack1.isEmpty()){
            stack2.push(stack1.pop());
        }
        
        //从stack2中弹栈得到结果
        for(int i = 0;i < count && !stack2.isEmpty(); i ++){
            result[i] = stack2.pop();
        }
        return result;
    }
}

3.实现一个栈的逆序,但是只能用递归函数和这个栈本身的pop操作来实现,而不能自己申请另外的数据结构。

public class SolutionThree {

    public static void main(String[] args) {
        
        Stack<Integer> s = new Stack<Integer>();
        /*s.push(1);
        s.push(2);
        s.push(3);*/
        
        
        //[230,272,224,399,126]
        s.addAll(Arrays.asList(230,272,224,399,126));
        
        SolutionThree st = new SolutionThree();
        st.reverse(s);
        
        s.forEach(i -> {
            System.out.print(i + " ");
        });
        
        
    }
    
    //利用下面的get实现栈元素的逆序
    public void reverse(Stack<Integer> stack) {
        if(stack.isEmpty()){
            return;
        }
        
        int i = get(stack);
        reverse(stack);
        stack.push(i);
    }
    
    
    //利用递归移除栈底元素并返回,只能使用栈自身的方法,
    //不能使用额外的数据结构
    public int get(Stack<Integer> stack) {
        int result = stack.pop();
        if(stack.isEmpty()) {
            return result;
        }else{
            int last = get(stack);
            stack.push(result);
            return last;
        }
        
    }
}

4.请编写一个程序,按升序对栈进行排序(即最大元素位于栈顶),
   要求最多只能使用一个额外的栈存放临时数据,但不得将元素复制到别的数据结构中。
   给定一个int[] numbers(C++中为vector<int>),其中第一个元素为栈顶,
   请返回排序后的栈。请注意这是一个栈,意味着排序过程中你只能访问到第一个元素。

public class SolutionFour {
    
    
    public static void main(String[] args) {
        SolutionFour f = new SolutionFour();
        
        int[] in = {54695,46580,6418,52304,5595,5149,51943,11454,23596,6444,61037,94146,50220,98642,97292,57898,11745,7286,31224,5160,41550,25277,59350,53353,68663,9642,30406,5396,3222,67194,7124,54247,15077,97688,36939,62888,80307,65467,6882,97071,39652,38268,88226,89088,92198,39003,9858,73803,83078,24648,49891,34551,57649,24443,30685,68740,55407,53155,87465,89282,41856,96218,37292,24551,67663,31715,46363,25573,61921,56333,69576,55919,19818,26409,21590,70392,67648,36909,89175,74443,41856,11755,24788,25975,25116,57360,80998,62093,40691,91189,29337,68914,57653,64272,53653,5975,27967,59600,25803,13937,93725,26457,16603,18360,79926,63243,94958,45131};
        f.twoStacksSort(in);
    }

    public ArrayList<Integer> twoStacksSort(int[] numbers) {
        
        ArrayList<Integer> result = new ArrayList<Integer>();
        
        
        Stack<Integer> stackData = new Stack<Integer>();
        Stack<Integer> stackHelp = new Stack<Integer>();
        for(int i = numbers.length - 1;i >= 0;i --){
            stackData.push(numbers[i]);
        }
        
        
        while(!stackData.isEmpty()){
            if(stackHelp.isEmpty()){
                stackHelp.push(stackData.pop());
            }else{
                int current = stackData.pop();
                if(current < stackHelp.peek()){
                    while(!stackHelp.isEmpty()){
                        stackData.push(stackHelp.pop());
                        if(!stackHelp.isEmpty() && current >= stackHelp.peek()){
                            stackHelp.push(current);
                            break;
                        }else if(stackHelp.isEmpty()){
                            stackHelp.push(current);
                            break;
                        }
                        
                        
                    }
                }else{
                    stackHelp.push(current);
                }
            }
        }
        
        stackHelp.forEach(i -> {
            System.out.print(i + "  ");
            result.add(i);
        });
        
        return result;
        
    }
    
}

 

posted on 2017-04-13 23:08  D.Zhang  阅读(706)  评论(0编辑  收藏  举报