【LeetCode刷题日记】栈类题目常见题型_leetcode 栈相关的题

image-20210906124138700

image-20210906124236801

#define LEN 20
typedef struct queue {
    int \*data;
    int head;
    int rear;
    int size;
} Queue;

typedef struct {
    Queue \*queue1, \*queue2;
} MyStack;

Queue \*initQueue(int k) {
    Queue \*obj = (Queue \*)malloc(sizeof(Queue));
    obj->data = (int \*)malloc(k \* sizeof(int));
    obj->head = -1;
    obj->rear = -1;
    obj->size = k;
    return obj;
}

void enQueue(Queue \*obj, int e) {
    if (obj->head == -1) {
        obj->head = 0;
    }
    obj->rear = (obj->rear + 1) % obj->size;
    obj->data[obj->rear] = e;
}

int deQueue(Queue \*obj) {
    int a = obj->data[obj->head];
    if (obj->head == obj->rear) {
        obj->rear = -1;
        obj->head = -1;
        return a;
    }
    obj->head = (obj->head + 1) % obj->size;
    return a;
}

int isEmpty(Queue \*obj) {
    return obj->head == -1;
}

MyStack \*myStackCreate() {
    MyStack \*obj = (MyStack \*)malloc(sizeof(MyStack));
    obj->queue1 = initQueue(LEN);
    obj->queue2 = initQueue(LEN);
    return obj;
}

void myStackPush(MyStack \*obj, int x) {
    if (isEmpty(obj->queue1)) {
        enQueue(obj->queue2, x);
    } else {
        enQueue(obj->queue1, x);
    }
}

int myStackPop(MyStack \*obj) {
    if (isEmpty(obj->queue1)) {
        while (obj->queue2->head != obj->queue2->rear) {
            enQueue(obj->queue1, deQueue(obj->queue2));
        }
        return deQueue(obj->queue2);
    }
    while (obj->queue1->head != obj->queue1->rear) {
        enQueue(obj->queue2, deQueue(obj->queue1));
    }
    return deQueue(obj->queue1);
}

int myStackTop(MyStack \*obj) {
    if (isEmpty(obj->queue1)) {
        return obj->queue2->data[obj->queue2->rear];
    }
    return obj->queue1->data[obj->queue1->rear];
}

bool myStackEmpty(MyStack \*obj) {
    if (obj->queue1->head == -1 && obj->queue2->head == -1) {
        return true;
    }
    return false;
}

void myStackFree(MyStack \*obj) {
    free(obj->queue1->data);
    obj->queue1->data = NULL;
    free(obj->queue1);
    obj->queue1 = NULL;
    free(obj->queue2->data);
    obj->queue2->data = NULL;
    free(obj->queue2);
    obj->queue2 = NULL;
    free(obj);
    obj = NULL;
}

-------------------------------------------------------------------------
class MyStack {
public:
    queue<int> queue1;
    queue<int> queue2;

    /\*\* Initialize your data structure here. \*/
    MyStack() {

    }

    /\*\* Push element x onto stack. \*/
    void push(int x) {
        queue2.push(x);
        while (!queue1.empty()) {
            queue2.push(queue1.front());
            queue1.pop();
        }
        swap(queue1, queue2);
    }
    
    /\*\* Removes the element on top of the stack and returns that element. \*/
    int pop() {
        int r = queue1.front();
        queue1.pop();
        return r;
    }
    
    /\*\* Get the top element. \*/
    int top() {
        int r = queue1.front();
        return r;
    }
    
    /\*\* Returns whether the stack is empty. \*/
    bool empty() {
        return queue1.empty();
    }
};

image-20210906124337842

typedef struct tagListNode {
    struct tagListNode\* next;
    int val;
} ListNode;

typedef struct {
    ListNode\* top;
} MyStack;

MyStack\* myStackCreate() {
    MyStack\* stk = calloc(1, sizeof(MyStack));
    return stk;
}

void myStackPush(MyStack\* obj, int x) {
    ListNode\* node = malloc(sizeof(ListNode));
    node->val = x;
    node->next = obj->top;
    obj->top = node;
}

int myStackPop(MyStack\* obj) {
    ListNode\* node = obj->top;
    int val = node->val;
    obj->top = node->next;
    free(node);

    return val;
}

int myStackTop(MyStack\* obj) {
    return obj->top->val;
}

bool myStackEmpty(MyStack\* obj) {
    return (obj->top == NULL);
}

void myStackFree(MyStack\* obj) {
    while (obj->top != NULL) {
        ListNode\* node = obj->top;
        obj->top = obj->top->next;
        free(node);
    }
    free(obj);
}

------------------------------------------------------------------------
class MyStack {
public:
    queue<int> q;

    /\*\* Initialize your data structure here. \*/
    MyStack() {

    }

    /\*\* Push element x onto stack. \*/
    void push(int x) {
        int n = q.size();
        q.push(x);
        for (int i = 0; i < n; i++) {
            q.push(q.front());
            q.pop();
        }
    }
    
    /\*\* Removes the element on top of the stack and returns that element. \*/
    int pop() {
        int r = q.front();
        q.pop();
        return r;
    }
    
    /\*\* Get the top element. \*/
    int top() {
        int r = q.front();
        return r;
    }
    
    /\*\* Returns whether the stack is empty. \*/
    bool empty() {
        return q.empty();
    }
};

42. 接雨水

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906124757834.png
https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906125808305.png

int trap(vector<int>& height)
{
    int ans = 0;
    int size = height.size();
    for (int i = 1; i < size - 1; i++) {
        int max_left = 0, max_right = 0;
        for (int j = i; j >= 0; j--) { //Search the left part for max bar size
            max_left = max(max_left, height[j]);
        }
        for (int j = i; j < size; j++) { //Search the right part for max bar size
            max_right = max(max_right, height[j]);
        }
        ans += min(max_left, max_right) - height[i];
    }
    return ans;
}

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906125846138.png

int trap(vector<int>& height)
{
    if (height == null)
        return 0;
    int ans = 0;
    int size = height.size();
    vector<int> left\_max(size), right\_max(size);
    left_max[0] = height[0];
    for (int i = 1; i < size; i++) {
        left_max[i] = max(height[i], left_max[i - 1]);
    }
    right_max[size - 1] = height[size - 1];
    for (int i = size - 2; i >= 0; i--) {
        right_max[i] = max(height[i], right_max[i + 1]);
    }
    for (int i = 1; i < size - 1; i++) {
        ans += min(left_max[i], right_max[i]) - height[i];
    }
    return ans;
}

在这里插入图片描述

int trap(vector<int>& height)
{
    int ans = 0, current = 0;
    stack<int> st;
    while (current < height.size()) {
        while (!st.empty() && height[current] > height[st.top()]) {
            int top = st.top();
            st.pop();
            if (st.empty())
                break;
            int distance = current - st.top() - 1;
            int bounded_height = min(height[current], height[st.top()]) - height[top];
            ans += distance \* bounded_height;
        }
        st.push(current++);
    }
    return ans;
}

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906130013086.png

int trap(vector<int>& height)
{
    int left = 0, right = height.size() - 1;
    int ans = 0;
    int left_max = 0, right_max = 0;
    while (left < right) {
        if (height[left] < height[right]) {
            height[left] >= left_max ? (left_max = height[left]) : ans += (left_max - height[left]);
            ++left;
        }
        else {
            height[right] >= right_max ? (right_max = height[right]) : ans += (right_max - height[right]);
            --right;
        }
    }
    return ans;
}

剑指 Offer 06. 从尾到头打印链表

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210906130306439.png
在这里插入图片描述

/\*\*
 \* Definition for singly-linked list.
 \* struct ListNode {
 \* int val;
 \* ListNode \*next;
 \* ListNode(int x) : val(x), next(NULL) {}
 \* };
 \*/
class Solution {
public:
    vector<int> reversePrint(ListNode\* head) {
        stack<int> s;
        vector<int> res;
        ListNode\* pre=head;
        while(pre){
            s.push(pre->val);
            pre=pre->next;
        }
        while(!s.empty()){
            res.push\_back(s.top());
            s.pop();
        }
        return res;
    }
};

剑指 Offer 09. 用两个栈实现队列

image-20210906130759985

image-20210906130923779

class CQueue {
    stack<int> stack1,stack2;
public:
    CQueue() {
        while (!stack1.empty()) {
            stack1.pop();
        }
        while (!stack2.empty()) {
            stack2.pop();
        }
    }
    
    void appendTail(int value) {
        stack1.push(value);
    }
    
    int deleteHead() {
        // 如果第二个栈为空
        if (stack2.empty()) {
            while (!stack1.empty()) {
                stack2.push(stack1.top());
                stack1.pop();
            }
        } 
        if (stack2.empty()) {
            return -1;
        } else {
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值