题解 | #删除字符串中出现次数最少的字符#

删除字符串中出现次数最少的字符

http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9

记得把无关的空格 trim 掉

use std::io;
use std::collections::HashMap;

fn main() {
    let mut s = String::new();
    let mut map:HashMap<u8,u8> = HashMap::new();
    io::stdin()
        .read_line(&mut s)
        .expect("Failed to read line");
    let mut v = Vec::from(s.trim());
    for i in v.iter() {
        let num = match map.get(i){
            Some(val) => *val + 1,
            None => 1u8,
        };
        map.insert(*i, num);
    }
    let (&_a,&b) = map.iter().min_by_key(|(_x,y)| **y ).unwrap();
    for i in map {
        if i.1 == b {
            for j in (0..v.len()).rev() {
                if v[j] == i.0 {
                    v.remove(j);
                }
            }
        }
    }
    for i in v {
        print!("{}",char::from(i));
    }
}

用 Rust 刷华为机试HJ 文章被收录于专栏

用 Rust 刷 HJ100 题,只需要懂基础 Rust 语法就能看懂

全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞 回复 分享
发布于 2022-04-20 17:26

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务