打字题解
因为涉及到回退操作,所以用栈模拟即可。
时间复杂度,空间复杂度
。
class Solution {
public:
/**
*
* @param s string字符串
* @return string字符串
*/
string Typing(string s) {
// write code here
stack<char>stk;
for(int i=0;i<s.size();i++) {
if(s[i]!='<') stk.push(s[i]);
else if(!stk.empty()) stk.pop();
}
string ans="";
while(!stk.empty()) {
ans+=stk.top();
stk.pop();
}
reverse(ans.begin(),ans.end());
return ans;
}
};
查看25道真题和解析
