当前位置: 首页 > news >正文

力扣396

模拟栈,没有存‘[’,无法通过下面这个测试用例,遂放弃。

#include <string> #include <stack> #include <iostream> using namespace std; class Solution { public: //将字符操作后再压入栈 string decodeString(string s) { stack<string> str; stack<int> num; string currStr = ""; string lastStr = ""; int n = 0; string res = ""; for(int i = 0;i < s.size();){ if(s[i] <= 'z' && s[i] >= 'a') { while(i < s.size() && s[i] <= 'z' && s[i] >= 'a'){ currStr.push_back(s[i]); i++; } str.push(currStr); currStr.clear(); } else if(s[i] >= '0' && s[i] <= '9'){ while(i < s.size() && s[i] >= '0' && s[i] <= '9'){ n = n*10 + s[i] - '0'; i++; } num.push(n); n = 0; } else if(s[i] == ']'){ int count = num.top(); num.pop(); string curr = ""; while(count--){ curr = curr + str.top(); } str.pop(); if(!str.empty()){ lastStr = str.top() + curr; str.pop(); } else{ lastStr = curr; } str.push(lastStr); i++; } else{ i++; } } //将字母栈还原 while(!str.empty()){ res = str.top() + res; str.pop(); } return res; } }; int main(){ Solution s; string str = "3[z]2[2[y]pq4[2[jk]e1[f]]]ef"; cout << s.decodeString(str) << endl; return 0; }

新写法,完全模拟,注意,最后要反转。

#include <string> #include <stack> #include <iostream> #include <algorithm> using namespace std; class Solution { public: //将字符操作后再压入栈 //‘[’压栈不能省 string decodeString(string s) { stack<char> str; stack<int> num; int n = 0; for(int i = 0;i < s.size();i++){ if(s[i] <= '9' && s[i] >= '0'){ n = n * 10 + s[i] - '0'; } else if(s[i] >= 'a' && s[i] <= 'z'){ str.push(s[i]); } else if(s[i] == '['){ num.push(n); n = 0; str.push('['); } else if(s[i] == ']'){ string curr = ""; while(str.top() != '['){ curr = str.top() + curr; str.pop(); } str.pop(); int currN = num.top(); num.pop(); while(currN--){ for(int i = 0;i < curr.size();i++){ str.push(curr[i]); } } } } string res = ""; while(!str.empty()){ res.push_back(str.top()); str.pop(); } reverse(res.begin(),res.end()); return res; }; }; int main(){ Solution s; string str = "3[z]2[2[y]pq4[2[jk]e1[f]]]ef"; cout << s.decodeString(str) << endl; return 0; }
http://www.jsqmd.com/news/615719/

相关文章:

  • 深度解析:红海云为何成为大中型企业首选HR数字化底座
  • OpenClaw+SecGPT-14B低成本方案:树莓派家庭安全中枢搭建
  • DS18B20多点温度采集驱动库设计与工业应用
  • 打理多个微信不用慌,告别切换内耗很简单
  • 碳纳米管的导电性、导热性到底有多好?
  • 大模型之Linux服务器部署大模型礁
  • OpenClaw智能监控:基于千问3.5-9B的7×24小时系统巡检
  • OpenClaw+Phi-3-mini-128k-instruct:法律文件比对与风险点标注系统
  • 基础算法-高精度:高精度减法
  • FastAPI子应用挂载:别再让root_path坑你一夜贾
  • SteerBot_TB6612:面向差速转向机器人的TB6612驱动Arduino库
  • 重塑供应链效能,中企销订货系统源码助力企业数字化突围
  • 进程通信与网络协议
  • Vue 3动画角色登录页:从创意到优化
  • 创建abb机器人机械装置————简易活塞
  • 双系统Linux死机解决方法
  • 四门课程,帮您转型AI产品经理
  • OpenClaw多模型切换技巧:Qwen3-14b_int4_awq与本地小模型协同作战
  • 2026年AI搜索问答优化天花板横评:5大源头厂家综合对比+采购避坑指南
  • OpenClaw错误处理机制:Phi-3-vision识别失败自动重试方案
  • 2026年,这家质保长且免拆治理烧机油的修理厂,究竟有何过人之处?
  • Java 25虚拟线程到底多快?实测10万QPS下内存占用下降73%、吞吐提升4.8倍,附压测脚本与GraalVM调优清单
  • 《数论探微:进阶版》(Arithmetic Tales: Advanced Edition)暗
  • HagiCode Desktop 混合分发架构解析:如何用 PP 加速大文件下载皇
  • ki1.me/cat/2 ai模型充值网站
  • 一文学习 工作流开发 BPMN、 Flowable钾
  • IOFILE结构体的介绍与House of orange瘫
  • 重构教育评价体系:OCRAutoScore智能阅卷系统的技术革新与实践路径
  • nvm-windows兼容性深度解析:系统适配与版本管理实践指南
  • CSS如何利用Grid重写老旧的表格布局