基础数据结构第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();
    }
};

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

相关推荐
玄鱼殇2 小时前
前端排序算法
算法·排序算法
tqs_123452 小时前
倒排索引数据结构
java·前端·算法
a程序小傲2 小时前
听说前端又死了?
开发语言·前端·mysql·算法·postgresql·深度优先
副露のmagic2 小时前
python基础复健
python·算法
bclshuai2 小时前
深度学习算法辅助股票分析
人工智能·深度学习·算法
bkspiderx2 小时前
RabbitMQ 技术指南(C/C++版)
c语言·c++·rabbitmq
mit6.8242 小时前
437贪心
算法
hetao17338372 小时前
2026-01-19~20 hetao1733837 的刷题笔记
c++·笔记·算法
梓䈑2 小时前
【Linux系统】实现线程池项目(含日志类的设计)
linux·服务器·c++