Leetcode每日一题 —— 3612. 用特殊操作处理字符串 I

魔法师 2026-06-16 08:56 1



思路

本来看完题还想着用什么结构减少操作次数,看看范围 (1 <= s.length <= 20) 和明天的题目 (多了k参数,类型都不同了),直接模拟吧。


代码


class Solution {
public String processStr(String s) {
StringBuilder sb = new StringBuilder();
char[] chars = s.toCharArray();
for (char chr : chars) {
if (chr == '*') {
if (!sb.isEmpty()) {
sb.deleteCharAt(sb.length() - 1);
}
} else if (chr == '#') {
sb.append(sb);
} else if (chr == '%') {
sb.reverse();
} else {
sb.append(chr);
}
}
return sb.toString();
}
}
最新回复 (8)
  • SomeBottle 06-16 09:33
    1

    纯模拟题,注意可能有坑,在弹出最后一个字符时注意字符串不能为空。


    class Solution {
    public:
    string processStr(string s) {
    // 当模拟题做
    string res;
    for(char c:s){
    switch(c){
    case '*':
    // 注意有坑,边界情况
    if(res.size()>0)
    res.pop_back();
    break;
    case '#':
    res.insert(res.end(),res.begin(),res.end());
    break;
    case '%':{
    int l=0,r=res.size()-1;
    while(l<r){
    swap(res[l],res[r]);
    l++;
    r--;
    }
    break;
    }
    default:
    res.push_back(c);
    }
    }
    return res;
    }
    };
  • Infinity4B 06-16 09:49
    2
    class Solution:
    def processStr(self, s: str) -> str:
    result=''
    for ss in s:
    if ss=='*' and len(result)>0:
    result=result[:-1]
    elif ss=='#':
    result=result+result
    elif ss=='%':
    result=result[::-1]
    elif ss.islower():
    result+=ss
    return result

    直接一个字符串随意操作

  • heart_of_god_lin 06-16 10:08
    3

    字符串比较短,字符串较长且反转多复制少的话可以拿双端队列+反转标志位,抠一抠反转的时间复杂度,虽然碰到复制还是得拼 ^-^

  • Lvvvv 06-16 10:24
    4

    提前预判明天不会写(


    class Solution {
    public:
    string processStr(string s) {
    std::string res;
    for(const auto& it : s) {
    if(it == '*') {
    int n = res.size();
    if(n) {
    res = res.substr(0,n - 1);
    }
    } else if(it == '#') {
    res += res;
    } else if(it == '%') {
    reverse(res.begin(),res.end());
    } else {
    res += it;
    }
    }
    return res;
    }
    };
  • CPython 06-16 13:48
    5
    class Solution:
    def processStr(self, s: str) -> str:
    ans = ''
    for c in s:
    if c == '#':
    ans += ans
    elif c == '%':
    ans = ans[::-1]
    elif c == '*':
    ans = ans[:-1]
    else:
    ans += c
    return ans
  • GreenOnion 06-16 14:36
    6

    3614提前做了 ^-^


    impl Solution {
    pub fn process_str(s: String, mut k: i64) -> char {
    let sb = s.as_bytes();
    let mut length = 0i64;
    for b in sb {
    length = match b {
    b'*' => 0.max(length - 1),
    b'#' => length * 2,
    b'%' => continue,
    _ => length + 1,
    }
    }
    if k >= length { return '.'; }
    for b in sb.iter().rev() {
    match b {
    b'*' => length += 1,
    b'#' => {
    length /= 2;
    if k >= length { k -= length; }
    },
    b'%' => {
    k = length - 1 - k;
    },
    _ => {
    if k == length - 1 { return *b as char; }
    length -= 1;
    }
    }
    }
    '.'
    }
    }
  • Qiansui 06-16 16:12
    7

    简单题简单做,模拟启动!


    class Solution {
    public:
    string processStr(string s) {
    string ans;
    for(char& ch : s){
    if(ch >= 'a' && ch <= 'z') ans.push_back(ch);
    else if(ch == '*'){
    if(ans.size()) ans.pop_back();
    }else if(ch == '#') ans += ans;
    else reverse(ans.begin(), ans.end());
    }
    return ans;
    }
    };
  • 咪帕 06-16 17:24
    8

    ^-^题


    class Solution {
    public:
    string processStr(string s) {
    string result;
    for (int i = 0; i < s.size(); i++) {
    if (islower(s[i])) {
    result += s[i];
    } else if (s[i] == '*' && !result.empty()) {
    result.pop_back();
    } else if (s[i] == '#') {
    result += result;
    } else {
    reverse(result.begin(), result.end());
    }
    }
    return result;
    }
    };

    好处是得知标准库有isalpha()、isalnum()、isdigit()、islower()和isupper()系列函数,可以直接用

* 帖子来源Linux.do
返回