基础数据结构第08天:栈(实战篇)

目录

进制转换

Bitset

[LCR 123. 图书整理 I - 力扣(LeetCode)](#LCR 123. 图书整理 I - 力扣(LeetCode))

[LCR 027. 回文链表 - 力扣(LeetCode)](#LCR 027. 回文链表 - 力扣(LeetCode))

[1614. 括号的最大嵌套深度 - 力扣(LeetCode)](#1614. 括号的最大嵌套深度 - 力扣(LeetCode))

[20. 有效的括号 - 力扣(LeetCode)](#20. 有效的括号 - 力扣(LeetCode))

[知识星球 | 深度连接铁杆粉丝,运营高品质社群,知识变现的工具](#知识星球 | 深度连接铁杆粉丝,运营高品质社群,知识变现的工具)


进制转换

cpp 复制代码
#include <iostream>
#include <stack>
using namespace std;
#define int long long
int n, x;
void solve()
{
    if (n == 0)
    {
        cout << 0 << endl;
        return;
    }
    if (n < 0)
    {
        cout << '-';
        n = -n;
    }
    stack<int> stk;
    while (n)
    {
        stk.push(n % x);
        n /= x;
    }
    while (!stk.empty())
    {
        int t = stk.top();
        stk.pop();
        if (t >= 10)
        {
            cout << (char)('A' + t - 10);
        }
        else
        {
            cout << t;
        }
    }
    cout << endl;
}
signed main()
{
    while (cin >> n >> x)
    {
        solve();
    }
    return 0;
}

Bitset

cpp 复制代码
#include <iostream>
#include <stack>
using namespace std;
#define int long long
int n;
void solve()
{
    if (n == 0)
    {
        cout << 0 << endl;
        return;
    }
    if (n < 0)
    {
        cout << '-';
        n = -n;
    }
    stack<int> stk;
    while (n)
    {
        stk.push(n % 2);
        n /= 2;
    }
    while (!stk.empty())
    {
        int t = stk.top();
        stk.pop();
        if (t >= 10)
        {
            cout << (char)('A' + t - 10);
        }
        else
        {
            cout << t;
        }
    }
    cout << endl;
}
signed main()
{
    while (cin >> n )
    {
        solve();
    }
    return 0;
}

LCR 123. 图书整理 I - 力扣(LeetCode)

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    vector<int> reverseBookList(ListNode* head) {
        vector<int> res;
        ListNode * t=head;
        while(t!=nullptr){
            res.push_back(t->val);
            t=t->next;
        }
        for(int i=0;i<res.size()/2;i++){
            swap(res[i],res[res.size()-i-1]);
        }
        return res;
    }
};

LCR 027. 回文链表 - 力扣(LeetCode)

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        vector<int> res;
        ListNode *t=head;
        while(t!=nullptr){
            res.push_back(t->val);
            t=t->next;
        }
        for(int i=0;i<res.size()/2;i++){
            if(res[i]!=res[res.size()-i-1]){
                return false;
            }
        }
        return true;
    }
};

1614. 括号的最大嵌套深度 - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    int maxDepth(string s) {
        int res = 0;
        int left = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '(') {
                res = max(res, ++left);
            } 
            else if (s[i] == ')') {
                left--;
            }
        }
        return res;
    }
};

20. 有效的括号 - 力扣(LeetCode)

cpp 复制代码
class Solution
{
public:
    bool isValid(string s)
    {
        unordered_map<char,char>map{
            {')', '('}, {']', '['}, {'}', '{'}
        };
        stack<int> stk;
        for (int i = 0;i<s.size();i++){
            if(map.count(s[i])){
                if(stk.empty()||stk.top()!=map[s[i]]){
                    return false;
                }
                stk.pop();
            }
            else
                stk.push(s[i]);
        }
        return stk.empty();
    }
};

知识星球 | 深度连接铁杆粉丝,运营高品质社群,知识变现的工具

相关推荐
灵感__idea13 分钟前
Hello 算法:众里寻她千“百度”
前端·javascript·算法
Wect10 小时前
LeetCode 130. 被围绕的区域:两种解法详解(BFS/DFS)
前端·算法·typescript
NAGNIP1 天前
一文搞懂深度学习中的通用逼近定理!
人工智能·算法·面试
端平入洛1 天前
delete又未完全delete
c++
颜酱1 天前
单调栈:从模板到实战
javascript·后端·算法
CoovallyAIHub1 天前
仿生学突破:SILD模型如何让无人机在电力线迷宫中发现“隐形威胁”
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
从春晚机器人到零样本革命:YOLO26-Pose姿态估计实战指南
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
强化学习凭什么比监督学习更聪明?RL的“聪明”并非来自算法,而是因为它学会了“挑食”
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
YOLO-IOD深度解析:打破实时增量目标检测的三重知识冲突
深度学习·算法·计算机视觉