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;
    }
};
相关推荐
一木 之林24 分钟前
五、C++ 新特性、关键字与编译原理(进阶)(二)
java·开发语言·c++
OPEN-F38 分钟前
C++11/14新特性精讲:移动语义与智能指针实战
开发语言·c++·算法
闭月之泪舞1 小时前
C++编程学习
c++·学习
lisin-lee-cooper1 小时前
【leetcode658】有序数组找出k个最接近x的数
java·数据结构·算法
sunburn-1 小时前
Java堆(Heap)详解与实战教学
java·开发语言·数据结构·ide·算法
智碳能碳管理平台2 小时前
工业能耗台账标准化:能碳管理系统的数据口径怎么设计
算法·能碳管理系统·智碳能碳管理平台·企业能碳管理系统·碳排放核算软件·绿色工厂申报saas·能碳管理平台
带多刺的玫瑰2 小时前
Leecode#15刷题之三数之和
算法·leetcode·职场和发展
圣保罗的大教堂2 小时前
leetcode 877. 石子游戏 中等
leetcode
哭泣方源炼蛊2 小时前
并查集进阶 P1(带权并查集,并查集分类)
数据结构·c++·算法·二进制·带权并查集
牧羊人.3333 小时前
动手学深度学习 02 | 手写数字识别
图像处理·人工智能·深度学习·算法