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

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

相关推荐
学涯乐码堂主5 小时前
有趣的“打擂台算法”
c++·算法·青少年编程·gesp
切糕师学AI5 小时前
环形缓冲区(Ring Buffer / Circular Buffer)详解:原理、优势、应用与高性能实现
数据结构·环形缓冲区
Tutankaaa5 小时前
知识竞赛题库设计全攻略
人工智能·算法
云栖梦泽6 小时前
Linux内核与驱动:14.SPI子系统
linux·运维·服务器·c++
WolfGang0073216 小时前
代码随想录算法训练营 Day50 | 图论 part08
数据结构·算法·图论
Gary Studio6 小时前
安卓HAL C++基础-智能指针
开发语言·c++
还是阿落呀6 小时前
基本控制结构2
c++
多思考少编码7 小时前
PAT甲级真题1001 - 1005题详细题解(C++)(个人题解)
c++·python·最短路·pat·算法竞赛
极客智造8 小时前
C++ 标准 IO 流全详解:cin /cout/get /getline 原理、用法、区别与避坑
c++·io
charlie1145141918 小时前
嵌入式C++工程实践第20篇:GPIO 输入模式内部电路 —— 芯片是如何“听“到外部信号的
开发语言·c++·stm32·单片机