[56] Using two stacks to implement a queue | Fibonacci sequence

Implement the queue with two stacks

Problem Description

Implement a queue with two stacks. The declaration of the queue is as follows. Please implement its two functions appendTail and deleteHead, which respectively complete the functions of inserting integers at the end of the queue and deleting integers at the head of the queue. (If there is no element in the queue, the deleteHead operation returns -1)

Problem-solving ideas

Stack
: Last In, First Out Queue: First In, First Out

Look directly from the official diagram:
Insert picture description here
use two stacks, stack 1 is used to store new data, stack 2 is used to reverse the data of stack 1, in order to achieve the purpose of first in, first out. Specifically: when appendTail, directly save the data to stack 1, when deleteHead, first check whether there are any elements in stack 2, if there are elements, then check whether there are elements in stack 1, and if there are elements in stack 1, first Pop and load stack 2 one by one, and pop stack 2 again. If there is no element in stack 1, the queue is empty.

Code:

class CQueue {
    
    
    Deque<Integer> stack1;
    Deque<Integer> stack2;
    
    public CQueue() {
    
     //构造函数
        stack1 = new LinkedList<Integer>();
        stack2 = new LinkedList<Integer>();
    }
    
    public void appendTail(int value) {
    
    
        stack1.push(value);
    }
    
    public int deleteHead() {
    
    
        if(!stack2.isEmpty()){
    
    
            int deleteItem = stack2.pop();
            return deleteItem;
        }else{
    
     // 若栈2为空
            while (!stack1.isEmpty()) {
    
     //若栈1不为空,则将栈1弹栈后的元素装进栈2
                stack2.push(stack1.pop());
            }
        } 
        if (stack2.isEmpty())  //若经过栈1的入栈后栈2还为空,则返回-1,说明队列为空
            return -1;
        else{
    
    
            int deleteItem = stack2.pop();
            return deleteItem;
        }
    }
}

/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue obj = new CQueue();
 * obj.appendTail(value);
 * int param_2 = obj.deleteHead();
 */

Fibonacci sequence

Problem Description

Write a function, enter n, and find the nth term of the Fibonacci sequence (ie F(N)). The definition of Fibonacci sequence is as follows:

F(0) = 0, F(1) = 1
F(N) = F(N-1) + F(N-2), where N> 1. The
Fibonacci sequence starts with 0 and 1, and the following The Fibonacci number is obtained by adding the previous two numbers.

The answer needs to be modulo 1e9+7 (1000000007). If the initial result of the calculation is: 1000000008, please return 1.

Problem-solving ideas

Use dynamic programming directly according to the formula, and recursion will time out.

class Solution {
    
    
    public int fib(int n) {
    
    
    	if(n == 0 || n == 1) return n;
        int dp[] = new int[n+1];
        dp[0] = 0;
        dp[1] = 1;
        for(int i=2;i<=n;i++)
            dp[i] = (dp[i-1] + dp[i-2])%1000000007; //注意题目要求取模
        return dp[n];
    }
}

Guess you like

Origin blog.csdn.net/qq_43424037/article/details/114985321