LeetCode75——Day24
最佳答案 问答题库498位专家为你答疑解惑
文章目录
- 一、题目
- 二、题解
一、题目
2390. Removing Stars From a String
You are given a string s, which contains stars *.
In one operation, you can:
Choose a star in s.
Remove the closest non-star character to its left, as well as remove the star itself.
Return the string after all stars have been removed.
Note:
The input will be generated such that the operation is always possible.
It can be shown that the resulting string will always be unique.
Example 1:
Input: s = “leet**cod*e”
Output: “lecoe”
Explanation: Performing the removals from left to right:
- The closest character to the 1st star is ‘t’ in “leet**code". s becomes "leecod*e”.
- The closest character to the 2nd star is ‘e’ in “leecode”. s becomes “lecod*e”.
- The closest character to the 3rd star is ‘d’ in “lecod*e”. s becomes “lecoe”.
There are no more stars, so we return “lecoe”.
Example 2:
Input: s = “erase*****”
Output: “”
Explanation: The entire string is removed, so we return an empty string.
Constraints:
1 <= s.length <= 105
s consists of lowercase English letters and stars *.
The operation above can be performed on s.
二、题解
利用栈
解决问题
class Solution {
public:string removeStars(string s) {int n = s.length();stack<char> st;string res = "";for(int i = 0;i < n;i++){char c = s[i];if(c != '*') st.push(c);else if(c == '*' && !st.empty()) st.pop();}while(st.size()) res += st.top(),st.pop();reverse(res.begin(),res.end());return res;}
};
99%的人还看了
相似问题
- 【洛谷 B2003】输出第二个整数 题解(顺序结构+输入输出)
- 20天拿下华为OD笔试之【模拟】2023B-数大雁【欧弟算法】全网注释最详细分类最全的华为OD真题题解
- Centos(Linux)服务器安装Dotnet8 及 常见问题解决
- 【蓝桥杯选拔赛真题23】C++计算24 第十二届蓝桥杯青少年创意编程大赛C++编程选拔赛真题解析
- [github配置] 远程访问仓库以及问题解决
- 电子学会C/C++编程等级考试2022年06月(一级)真题解析
- [github初学者教程] 分支管理-以及问题解决
- Flume的安装部署及常见问题解决
- 电子学会C/C++编程等级考试2022年03月(一级)真题解析
- 2023年咸阳市《网络建设与运维》赛题解析------四、安全配置
猜你感兴趣
版权申明
本文"LeetCode75——Day24":http://eshow365.cn/6-30969-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: [AUTOSAR][诊断管理][ECU][$31] 例程控制
- 下一篇: 前端基础之BOM和DOM