题目链接:https://leetcode-cn.com/problems/make-the-string-great/
题目如下:
class Solution {
public:
string makeGood(string s) {
stack<char> stk;
string result = "";
for (int i = 0; i<s.length(); i++) {
if (!stk.empty()) {
char ch1 = stk.top();
char ch2 = s[i];
if (abs(ch1 - ch2) == 32) stk.pop();
else stk.push(s[i]);
}
else {
stk.push(s[i]);
}
}
for (int i = 0; i<stk.size();/*i++*/) {//最开始没去除i++,拼接结果不符合要求
result=stk.top()+result;
//cout << result<< endl;
stk.pop();
}
return result;
}
};
// class Solution{
// public:
// string makeGood(string s) {
// string res="";
// for (int i=0;i<s.size()-1;) {
// if ((s[i] >= 'a' && s[i] <= 'z') && (s[i+1] >= 'A' && s[i+1] <= 'Z')) {
// i+=2;
// }
// else if ((s[i] >= 'A' && s[i] <= 'Z') && (s[i+1] >= 'a' && s[i+1] <= 'z')) {
// i+=2;
// }
// else {
// res += s[i];
// ++i;
// }
// }
// return res;
// }
// };