LeetCode75——Day24

文章目录

一、题目

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.

二、题解

利用解决问题

cpp 复制代码
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;
    }
};
相关推荐
十年编程老舅5 分钟前
Linux NUMA架构深度剖析:内存管理、进程调度与性能优化
linux·数据库·c++·内存管理·numa
少司府10 分钟前
C++基础入门:深挖list的那些事
开发语言·数据结构·c++·容器·list·类型转换·类和对象
罗超驿15 分钟前
14.MySQL索引底层原理:从数据结构到B+树的深度解析
数据结构·b树·mysql
孬甭_16 分钟前
单链表详解
c语言·数据结构
khalil102017 分钟前
代码随想录算法训练营Day-53 图论01 | 110.字符串接龙、105.有向图的完全可达性、106.岛屿的周长
算法
IronMurphy20 分钟前
【算法四十六】300. 最长递增子序列
算法
碧海银沙音频科技研究院26 分钟前
高通QCC3084-QCC518X蓝牙耳机项目
人工智能·深度学习·算法
兩尛32 分钟前
compare_exchange_weak 的用法
算法
数智工坊32 分钟前
面向具身操作的视觉-语言-动作模型:让机器人真正理解并执行人类指令
论文阅读·人工智能·算法·机器人
诙_32 分钟前
C++学习总结
开发语言·c++·学习