为class Solution { public: String removeDuplicateLetters(String s) { Vector<int> count(26); Vector<bool> inStack(26); Stack<char> st; for (std::size_t i = 0; i < s.size(); i++) { count[s[i] - 'a']++; } for (std::size_t i = 0; i < s.size(); i++) { count[s[i] - 'a']--; if (inStack[s[i] - 'a']) { continue; } while (!st.empty() && st.top() > s[i] && count[st.top() - 'a'] > 0) { inStack[st.top() - 'a'] = false; st.pop(); } st.push(s[i]); inStack[s[i] - 'a'] = true; } String ans; while (!st.empty()) { ans.push_back(st.top()); st.pop(); } ans.reverse(); return ans; } };写一个cpp源文件
时间: 2023-12-03 19:43:54 浏览: 95
5.8, 2.6, 4.0, 1.2},
{5.0, 2.3, 3.3, 1.0},
{5.6, 2.7, 4.2, 1抱歉,我无法在此处运行代码,但是我可以为你提供一个完整的C++源代码.3},
{5.7, 3.0, 4.2, 1.2},
{5.7,你可以将其复制到你的本地开发环境中,然后编译和运行它。
```cpp, 2.9, 4.2, 1.3},
{6.2, 2.9, 4
#include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
class Solution {
public:
string remove.3, 1.3},
{5.1, 2.5, 3.0, 1.1},
DuplicateLetters(string s) {
vector<int> count(26);
vector<bool> inStack(26);
stack<char> st;
{5.7, 2.8, 4.1, 1.3},
{6.3, 3 for (size_t i = 0; i < s.size(); i++) {
count[s[i] - 'a']++;
.3, 6.0, 2.5},
{5.8, 2.7, 5.1, }
for (size_t i = 0; i < s.size(); i++) {
count[s[i] - 'a']--;
1.9},
{7.1, 3.0, 5.9, 2.1},
{6 if (inStack[s[i] - 'a']) {
continue;
}
while (!st.empty() && st.top() >.3, 2.9, 5.6, 1.8},
{6.5, 3.0, s[i] && count[st.top() - 'a'] > 0) {
inStack[st.top() - 'a'] 5.8, 2.2},
{7.6, 3.0, 6.6, 2.1},
{4.9, 2.5, 4.5, 1.7},
{7.3, = false;
st.pop();
}
st.push(s[i]);
inStack[s[i] - 'a'] = true;
2.9, 6.3, 1.8},
{6.7, 2.5, 5. }
string ans;
while (!st.empty()) {
ans.push_back(st.top());
st.pop();
}
reverse(ans.begin8, 1.8},
{7.2, 3.6, 6.1, 2.5},
(), ans.end());
return ans;
}
};
int main() {
Solution solution;
string s = "bcabc";
string {6.5, 3.2, 5.1, 2.0},
{6.4, 2. ans = solution.removeDuplicateLetters(s);
cout << ans << endl;
return 0;
}
```
在此示例中,7, 5.3, 1.9},
{6.8, 3.0, 5.5, 我们实例化了一个Solution类,并使用removeDuplicateLetters方法从字符串“bcabc”中删除重复字母,将结果打印到控制台。
阅读全文