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

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

相关推荐
2401_831824968 分钟前
基于C++的区块链实现
开发语言·c++·算法
We་ct15 分钟前
LeetCode 918. 环形子数组的最大和:两种解法详解
前端·数据结构·算法·leetcode·typescript·动态规划·取反
愣头不青19 分钟前
238.除了自身以外数组的乘积
数据结构·算法
汉克老师41 分钟前
GESP5级C++考试语法知识(六、链表(一)单链表)
c++·链表·单链表·快慢指针·进阶·gesp5级·gesp五级
人工智能AI酱1 小时前
【AI深究】逻辑回归(Logistic Regression)全网最详细全流程详解与案例(附大量Python代码演示)| 数学原理、案例流程、代码演示及结果解读 | 决策边界、正则化、优缺点及工程建议
人工智能·python·算法·机器学习·ai·逻辑回归·正则化
WangLanguager1 小时前
逻辑回归(Logistic Regression)的详细介绍及Python代码示例
python·算法·逻辑回归
m0_518019481 小时前
C++与机器学习框架
开发语言·c++·算法
一段佳话^cyx1 小时前
详解逻辑回归(Logistic Regression):原理、推导、实现与实战
大数据·算法·机器学习·逻辑回归
qq_417695051 小时前
C++中的代理模式高级应用
开发语言·c++·算法
学嵌入式的小杨同学1 小时前
STM32 进阶封神之路(十九):ADC 深度解析 —— 从模拟信号到数字转换(底层原理 + 寄存器配置)
c++·stm32·单片机·嵌入式硬件·mcu·架构·硬件架构