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

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

相关推荐
Z1Jxxx2 分钟前
C++ P1150 Peter 的烟
数据结构·c++·算法
是娇娇公主~3 分钟前
线程池:工作窃取线程池WorkingStealingPool
c++·线程池
CheerWWW4 分钟前
C++学习笔记——函数指针、Lambda表达式、谨慎使用using namespace std、命名空间
c++·笔记·学习
夜猫子ing4 分钟前
如何编写一个CMakelists文件
开发语言·c++
踮起脚看烟花10 分钟前
chapter10_泛型算法
c++·算法
笨笨饿10 分钟前
# 52_浅谈为什么工程基本进入复数域?
linux·服务器·c语言·数据结构·人工智能·算法·学习方法
Code-keys10 分钟前
ADSP/ARM 性能/稳定性排查专栏总述
arm开发·算法·边缘计算·dsp开发
山栀shanzhi14 分钟前
C++四大常见排序对比
c++·算法·排序算法
云栖梦泽19 分钟前
Linux内核与驱动:8.ioctl驱动基础
linux·c++
Allen_LVyingbo28 分钟前
量子测量三部曲:投影测量、POVM 与坍缩之谜—从形式主义到物理图像
算法·性能优化·健康医疗·量子计算·空间计算