C++每日刷题day2025.7.10

思路就是遇到数字就push进栈,遇到符号就取栈顶俩元素进行对应符号运算再push进栈中,直到栈中只剩一个数字。

cpp 复制代码
class Solution {
public:
    bool isNumber(string& x)
    {
        if (x == "+" || x == "-" || x == "*" || x == "/")
            return false;
        else return true;
    }
    int evalRPN(vector<string>& tokens) {
        stack<int> st;
        for (int i = 0; i < tokens.size(); ++i)
        {
            string& x = tokens[i];
            if (isNumber(x)) st.push(atoi(x.c_str()));
            else{
                int num1 = st.top();
                st.pop();
                int num2 = st.top();
                st.pop();
                switch (x[0])
                {
                    case '+' :{
                        st.push(num1 + num2);
                        break;
                    }
                    case '-' :{
                        st.push(num2 - num1);
                        break;
                    }
                    case '*' :{
                        st.push(num1 * num2);
                        break;
                    }
                    case '/' :{
                        st.push(num2 / num1);
                        break;
                    }
                    default:
                        break;
                }
            }
        }
        return st.top();
    }
};

来个辅助栈,一个正常的栈,另一个是只存放最小值的栈。

cpp 复制代码
class MinStack {
public:
    stack<int> st;
    stack<int> min_st;
    MinStack() {
        min_st.push(INT_MAX);
    }
    
    void push(int val) {
        st.push(val);
        min_st.push(min(val, min_st.top()));
    }
    
    void pop() {
        st.pop();
        min_st.pop();
    }
    
    int top() {
        return st.top();
    }
    
    int getMin() {
        return min_st.top();
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(val);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */

我是用了一个堆一个优先级队列,优先级队列用来存放偶数的,栈用来存放奇数的,每次只需要取优先级队列/2再判断是不是偶数即可。

cpp 复制代码
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
using namespace std;

int main() {
    long long n, k;
    cin >> n >> k;
    
    // 使用 vector 动态分配内存
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    priority_queue<long long> st;
    stack<long long> other;
    for (long long i = 0; i < n; i++) {
        if (a[i] % 2 == 0) {
            st.push(a[i]);
        } else {
            other.push(a[i]);
        }
    }

    while (st.size() && k--) {
        long long top = st.top();
        st.pop();
        top /= 2;
        if (top % 2 == 0) st.push(top);
        else other.push(top);
    }

    long long sum = 0;
    while (!st.empty()) {
        sum += st.top();
        st.pop();
    }
    while (!other.empty()) {
        sum += other.top();
        other.pop();
    }
    cout << sum;

    return 0;
}
相关推荐
代码游侠2 分钟前
学习笔记——HC-SR04 超声波测距传感器
开发语言·笔记·嵌入式硬件·学习
superman超哥14 分钟前
Context与任务上下文传递:Rust异步编程的信息高速公路
开发语言·rust·编程语言·context与任务上下文传递·rust异步编程
橘颂TA14 分钟前
【剑斩OFFER】算法的暴力美学——leetCode 946 题:验证栈序列
c++·算法·leetcode·职场和发展·结构与算法
步达硬件15 分钟前
【Matlab】批量自定义图像处理
开发语言·matlab
闻缺陷则喜何志丹16 分钟前
【状态机动态规划】3686. 稳定子序列的数量|1969
c++·算法·动态规划·力扣·状态机动态规划
军军君0117 分钟前
Three.js基础功能学习七:加载器与管理器
开发语言·前端·javascript·学习·3d·threejs·三维
liulilittle19 分钟前
OPENPPP2 网络驱动模式
开发语言·网络·c++·网络协议·信息与通信·通信
mjhcsp22 分钟前
C++ AC 自动机:原理、实现与应用全解析
java·开发语言·c++·ac 自动机
huihuihuanhuan.xin23 分钟前
后端八股之java并发编程
java·开发语言
寻星探路27 分钟前
【算法通关】双指针技巧深度解析:从基础到巅峰(Java 最优解)
java·开发语言·人工智能·python·算法·ai·指针