leetcode6202. 使用机器人打印字典许最小的字符串
- s中只包含小写字母,可以从小到大枚举字母
- 如果要将当前字母全部输出,则肯定需要先将s中包含的字母先操作的t中,所以先预处理s中每个字母最后出现的位置
| class Solution { |
| public: |
| string robotWithString(string s) { |
| int n = s.size(); |
| string res, t; |
| unordered_map<char, int> cim; |
| |
| for(int i = 0; i < n; i ++) |
| cim[s[i]] = i; |
| int k = 0; |
| for(int i = 0; i < 26; i ++) { |
| char c = i + 'a'; |
| if(cim.count(c) == 0) continue; |
| |
| while(t.size() && t.back() <= c) { |
| res += t.back(); |
| t.pop_back(); |
| } |
| |
| |
| while(k <= cim[c]) { |
| if(s[k] == c) res += c; |
| else t += s[k]; |
| k ++; |
| } |
| } |
| |
| |
| reverse(t.begin(), t.end()); |
| return res + t; |
| } |
| }; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】