iT邦幫忙

2024 iThome 鐵人賽

DAY 26
0
自我挑戰組

Leetcode 解題之旅:逐日攻克系列 第 26

每日一LeetCode(26)

  • 分享至 

  • xImage
  •  

3174. Clear Digits

題目敘述:

You are given a string s.

Your task is to remove all digits by doing this operation repeatedly:

Delete the first digit and the closest non-digit character to its left.
Return the resulting string after removing all digits.

class Solution {
public:
    string clearDigits(string s) {
        int i = 0;
        int n = s.length();
        while (i < n) {
            if (s[i] <= '9' && s[i] >= '0') {
                s[i] = '!'; 
                for (int j = i - 1; j >= 0; j--) {
                    if ((s[j] > '9' || s[j] < '0') && s[j] != '!') {
                        s[j] = '!'; 
                        break;
                    }
                }
            }
            i++;
        }
        string res = "";
        for (int k = 0; k < n; k++) {
            if (s[k] != '!') res += s[k];
        }
        return res;
    }
};


上一篇
每日一LeetCode(25)
下一篇
每日一LeetCode(27)
系列文
Leetcode 解題之旅:逐日攻克30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言