今日题目

https://leetcode.com/problems/clear-digits/description/

解题思路

本题需要将数字前面的字符弹出。

解题方法

因此,只需使用一个栈即可。

复杂度分析

  • 时间复杂度:$O(N)$,N 为字符串长度。

  • 空间复杂度:$O(N)$,N 为字符串长度。

代码

class Solution:
    def clearDigits(self, s: str) -> str:
        st = []
        for ch in s:
            if '0' <= ch and ch <= '9':
                st.pop()
            else:
                st.append(ch)
        
        return ''.join(st)