当前位置: 首页 > news >正文

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.

二、题解

利用解决问题

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;}
};
http://www.lryc.cn/news/216008.html

相关文章:

  • B端企业形象设计的正确姿势,你学会了吗?
  • 我在Vscode学OpenCV 基本的加法运算
  • 数据结构与算法解析(C语言版)--线性表
  • pthread 名字设置及线程标识符获取
  • 17、Flink 之Table API: Table API 支持的操作(1)
  • Ubuntu:解决PyCharm中不能输入中文或者输入一个中文解决方法
  • Vue3.0 reactive与ref :VCA模式
  • 项目实战 | 使用Linux宝塔面板搭建商城公众号小程序基础框架
  • IDEA远程调试代码
  • 目标检测 图像处理 计算机视觉 工业视觉
  • 【1day】宏景OA get_org_tree.jsp接口SQL注入漏洞学习
  • 设计模式-迭代子模式
  • 绿色通道 快速理赔,渤海财险用实干书写服务品牌
  • 微信小程序怎么制作?【小程序开发平台教学】
  • HTML、CSS和JavaScript,实现换肤效果的原理
  • 2103. 环和杆
  • YOLOv5:修改backbone为SPPCSPC
  • css中flexbox和grid的区别
  • uniapp循环对象列表---点击列表切换选中不同状态
  • 【使用Python编写游戏辅助工具】第二篇:键盘监听的应用
  • Shiny Server和ShinyProxy是什么,有什么区别?
  • Java 客户端、服务端NIO大文件传输
  • Unity3D与iOS的交互 简单版开箱即用
  • 限制LitstBox控件显示指定行数的最新数据(3/3)
  • Maven进阶系列-仓库和镜像
  • mac下载安装jenkins
  • Mac上的iTerm2和Oh My Zsh 的安装(安装过程和失败详解)
  • 阿里云OS系统Alibaba Cloud Linux 3系统的安全更新命令
  • 你写的Python代码到底多快?这些测试工具了解了解
  • 网际控制报文协议ICMP