Algorithm camp day5

Niuke deletes duplicate nodes in the linked list

Title description
In a sorted linked list, there are duplicate nodes. Please delete the duplicate nodes in the linked list. The duplicate nodes will not be retained, and the head pointer of the linked list will be returned. For example, the linked list 1->2->3->3->4->4->5 is processed as 1->2->5.
Idea:
Define two pointers prev and last, compare last and last->next If the value is not equal, prev++ last++
If the two values ​​are equal, then only let last++
pre->next = last->next
PS: When accessing the linked list, pay attention to the code to check whether the current node is empty
:

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
    
    
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
    
    
        if(nullptr == pHead){
    
    
            return pHead;
        }
        //加头结点
        ListNode *head  = new ListNode(0);
        head->next = pHead;
        ListNode *prev = head;//初始化指向dummy head
        ListNode *last = pHead;//初始化指向真正的头结点
        while(last){
    
    
            while(last->next != nullptr && last->val != last->next->val){
    
    
                prev = prev->next;
                last = last->next;
            }
            while(last->next != nullptr && last->val == last->next->val){
    
    
                last = last->next;
            }
            //走到这里结果一共有三种,注意:prev永远指向的是前驱有效节点:
            //1. last->next != nullptr 并且 (prev, last] 限定了一段重复范围,此时进行去重
            //2. last->next == nullptr && (prev, last] 限定了一段重复范围,此时进行去重,最后相当于prev->next = nullptr
            //3. last->next == nullptr && prev->next == last,这说明,从本次循环开始,大家都不相同,就不需要进行去重,这个是特殊情况
            if(prev->next != last){
    
    
                prev->next = last->next;
            }
            last = last->next;
        }
        return head->next;
    }
};

Niuke contains the min function stack

Title description
Define the data structure of the stack. Please implement a min function in this type that can get the smallest element contained in the stack (time complexity should be O(1)).
Idea:
Use an auxiliary stack to save the minimum value of all the numbers in the current stack.
In the push operation, if minstack is empty or value<minstack.top(), then value is put into minstack; otherwise, minstack.top() is repeatedly put into minstack.
Code:

class Solution {
    
    
stack<int> dataStack;
stack<int> minStack;
public:
    void push(int value) {
    
    
        if(minStack.empty() || value < minStack.top()){
    
    
            minStack.push(value);
        }
        else{
    
    
            minStack.push(minStack.top());
        }
        dataStack.push(value);
    }
    void pop() {
    
    
        minStack.pop();
        dataStack.pop();
    }
    int top() {
    
    
        return dataStack.top();
    }
    int min() {
    
    
        return minStack.top();
    }
};

Press-in and pop-up sequence of Niu Inn

Title description
Enter two integer sequences. The first sequence indicates the order in which the stack is pushed. Please judge whether the second sequence is the pop-up order of the stack. Assume that all numbers pushed onto the stack are not equal. For example, the sequence 1,2,3,4,5 is the push order of a certain stack, the sequence 4,5,3,2,1 is a pop sequence corresponding to the push sequence, but 4,3,5,1,2 It cannot be the pop sequence of the push sequence. (Note: the lengths of these two sequences are equal)

Idea:
Use a stack to simulate.
Code:

class Solution {
    
    
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
    
    
        if(pushV.empty() || popV.empty() || pushV.size() != popV.size())
            return false;
        stack<int> st;//辅助栈,帮助模拟入栈出栈
        int i = 0,j = 0;
        for(;i<pushV.size();++i){
    
    
            st.push(pushV[i]);
            //相等的话,表示要开始模拟出栈了
            while(!st.empty() && st.top() ==  popV[j]){
    
    //????
                st.pop();
                ++j;
            }
        }
        return st.empty();
    }
};

Niuke prints a binary tree from top to bottom

Title description
Print out each node of the binary tree from top to bottom, and print the nodes of the same level from left to right.

Idea: Use the
stack to implement BFS. PS: Remember the pop() code after taking the front
:

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
    
    
public:
    vector<int> PrintFromTopToBottom(TreeNode* root) {
    
    
        queue<TreeNode*> que;
        vector<int> ans;
        if(nullptr == root)
            return ans;
        que.push(root);
        while(!que.empty()){
    
    
            TreeNode *temp = que.front();
            que.pop();//你以为自己写的JAVA代码么???
            ans.push_back(temp->val);
            if(temp->left)
                que.push(temp->left);
            if(temp->right)
                que.push(temp->right);
        }
        return ans;
    }
};

Niuke Binary Tree Follow-up Traversal Sequence

Title description
Enter an integer array to determine whether the array is the result of a post-order traversal of a binary search tree. If yes, output Yes, otherwise output No. Assume that any two numbers in the input array are different from each other.
Idea:
Code:

class Solution {
    
    
public:
    bool helper(vector<int>& sequence, int start, int end){
    
    
        if(start >= end)
            return true;
        int root = sequence[end];
        int i = start;
        while(i<end && sequence[i] < root)
            ++i;
        for(int j=i;j<end;++j){
    
    
            if(sequence[j] < root)
                return false;
        }
        //判断左子树和右子树是否符合要求
        return helper(sequence, 0, i-1) &&
            helper(sequence, i, end-1);
    }
    bool VerifySquenceOfBST(vector<int> sequence) {
    
    
        if(sequence.empty()){
    
    
            return false;
        }
        return helper(sequence, 0, sequence.size()-1);
    }
};

Guess you like

Origin blog.csdn.net/qq_35353824/article/details/107561909