第14天 - 栈

前言

        打卡贴(第14天)
        今日内容:

一、练习题目

    题目一:LeetCode | 1441. 用栈操作构建数组|★☆☆☆☆
    题目二:LeetCode | 1021. 删除最外层的括号|★☆☆☆☆
    题目三:LeetCode | 1700. 无法吃午餐的学生数量|★☆☆☆☆
    题目四:LeetCode | 1381. 设计一个支持增量操作的栈|★☆☆☆☆

二、代码和思路

题目一

  1. 用栈操作构建数组
    https://leetcode.cn/problems/build-an-array-with-stack-operations/submissions/
  • (1)查找每一行第一次出现负数的下标
  • (2)(列数 - 下标) 即为当前行的负数个数;
class Solution {
public:
    vector<string> buildArray(vector<int>& target, int n) {
        vector<string> ans;
        int t[101];
        int i;
        int e;
        memset(t, 0, sizeof(t));
        for(i = 0; i < target.size(); ++i){
            ++t[ target[i] ];
        }
        e = target[i - 1];
        for(i = 1; i <= e; ++i){
            ans.push_back("Push");
            if(!t[i]){
                ans.push_back("Pop");
            }
        }
        return ans;
    }
};

在这里插入图片描述

题目二

  1. 删除最外层的括号
    https://leetcode.cn/problems/remove-outermost-parentheses/
  • (1)设置两个变量 l ,r 分别记录左括号和右括号的数量
  • (2)扫描从左往右字符,若为 ‘(’,且 l - r ! = 1,则存入ans;
  • (3)若为 ‘)’,且 l - r ! = 0,则存入ans;
class Solution {
public:
    string removeOuterParentheses(string s) {
        int l = 0, r = 0;
        string ans = "";
        int i;
        if(s.empty()) return "";
        for(i = 0; i < s.length(); ++i){
            if(s[i] == '('){
                ++l;
                if(l - r != 1){
                    ans += s[i];
                }
            }else if(s[i] == ')'){
                ++r;
                if(l - r != 0){
                    ans += s[i];
                }
            } 
        }
        return ans;
    }
};

在这里插入图片描述

题目三

  1. 无法吃午餐的学生数量
    https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/
  • (1)标记当前在最顶上的面包top和队头的同学head
  • (2)若tophead的值相同,则head指向的值赋为2tophead自增;否则,仅head自增。取模长度。
  • (3)i 计数到零则剩下的学生都不吃顶上的三明治
class Solution {
public:
    int countStudents(vector<int>& students, vector<int>& sandwiches) {
        int top = 0, head = 0;
        int n = students.size();
        int ans = n;
        int i = n;
        while(true){
            if(students[head] == sandwiches[top]){
                students[head] = 2;
                i = n;
                --ans;
                ++head;
                ++top;
            }else{
                ++head;
            }
            --i;
            if(i <= 0 || !ans){
                break;
            }
            head = (head) % (n);
        }
        return ans;
    }
};

在这里插入图片描述

题目四

  1. 设计一个支持增量操作的栈
    https://leetcode.cn/problems/design-a-stack-with-increment-operation/
  • 想用链表做的,目前实力不允许
    在这里插入图片描述
  • 顺序表实现
class CustomStack {
    int top, size;
    int *stk;
public:
    CustomStack(int maxSize) {
        stk = new int[maxSize + 1];
        size = maxSize;
        top = 0;
    }
    
    void push(int x) {
        if(top < size){
            stk[top++] = x;
        }
    }
    
    int pop() {
        if(top == 0){
            return -1;
        }
        return stk[--top];
    }
    
    void increment(int k, int val) {
        int i;
        for(i = 0; i < k && i < top; ++i){
            stk[i] += val;
        }
    }
};
/**
 * Your CustomStack object will be instantiated and called as such:
 * CustomStack* obj = new CustomStack(maxSize);
 * obj->push(x);
 * int param_2 = obj->pop();
 * obj->increment(k,val);
 */

在这里插入图片描述

其他练习题目

LeetCode 206. 反转链表
https://leetcode.cn/problems/reverse-linked-list/
LeetCode 20. 有效的括号
https://leetcode.cn/problems/valid-parentheses/
LeetCode 32. 最长有效括号
https://leetcode.cn/problems/longest-valid-parentheses/
LeetCode 234. 回文链表
https://leetcode.cn/problems/palindrome-linked-list/
LeetCode 682. 棒球比赛
https://leetcode.cn/problems/baseball-game/
LeetCode 844. 比较含退格的字符串
https://leetcode.cn/problems/backspace-string-compare/

ps: 编写于五月14日;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值