题解 | #点击消除#

点击消除

https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5

#include <iostream>
#include <iterator>
#include <memory>
using namespace std;
#include<stack>
#include<algorithm>

int main() {
    // int a, b;
    // while (cin >> a >> b) { // 注意 while 处理多个 case
    //     cout << a + b << endl;
    // }
    string a;
    cin >> a;
    stack<char> sta;
    for (int i = 0; i < a.size(); i++) //遍历字符串
    { 
        if (sta.empty()) 
        {
            sta.push(a[i]); //空栈下进栈
        } 
        else //非空栈
        { 
            if (sta.top() == a[i]) //栈顶与当前字符一样
            { 
                sta.pop();  //出栈
            } 
            else sta.push(a[i]); //不一样就进栈
        }
    }
    if (sta.empty()) cout << 0; //最终空栈输出0
    else 
    {
        string str;
        while (!sta.empty()) //出栈赋给字符串str
        { 
            str += sta.top();
            sta.pop();
        }
        reverse(str.begin(), str.end()); //反转字符串
        cout << str;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

小浪_Coding:1. 个人技能排版太乱, 写的技术栈太浅了, 跟测试,自动化相关的太少; 2. 项目开发类的太简单没有亮点, 算法类的项目建议只放一个,最好有自动化,CI/CD, pipline的项目, 需要更换; 3.整体排版需要优化, SOOB打招呼都需要注意等.
我的简历长这样
点赞 评论 收藏
分享
钱嘛数字而已:辅导员肯定不能同意,不然你出事了,他要承担责任。但是,脚和脑子都长在你自己身上,使用它还需要向辅导员报告么? 辅导员必须按流程拒绝你,然后你拿出成年人的态度,做自己的选择。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务