题解 | #点击消除# | Rust

点击消除

https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5

use std::io::{self, *};

struct Solution {}

impl Solution {
    pub fn clearStr(self, input: String) -> String {
        let mut ans = String::from("");
        for c in input.chars() {
            if !ans.is_empty() && c == ans.as_bytes()[ans.len()-1] as char {
                ans.pop();
                continue;
            }
            ans.push(c);
        }
        if ans.is_empty() {
            return String::from("0");
        }
        return ans;
    }
}

fn main() {
    let mut input = String::new();
    io::stdin().read_line(&mut input).unwrap();
    let mut input = input.trim_end().to_string();
    println!("{}", Solution{}.clearStr(input));
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-20 20:30
工作没了,落户没了,什么都没了
梦想是成为七海千秋:是因为什么原因呀,如果是因为导师恶意卡你就和他爆了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务