去年华为机试题目学习

************************************************************************

字符串压缩

按"分割后 单数索引的地方就是需要替换的字符串


fn string_compress(string: String, dict: Vec<String>) -> String {

    // ------- " ====== " ------------ " ===== " ---- 
    let chars = string.chars();
    let mut v = vec![];
    chars.enumerate().for_each(|(idx, char)| {
        if char == '\"' {
            v.push(idx);
        }
    });
    for char in string.chars() {
        if char == '\"' {

        }
    }

    let mut flag = false;

    if string.contains('\"') {
        flag = true;    
    }

    let mut strings = string.split('\"')
                            .map(|str| str.to_string().to_ascii_lowercase())
                            .collect::<Vec<String>>();

    let mut hm = HashMap::new();
    for (idx, string) in strings.iter().enumerate() {
        if idx % 2 == 0 {
            let mut new_string = string.clone();
            for (i, word) in dict.iter().enumerate() {
                new_string = new_string.replace(word.as_str(), i.to_string().as_str());
            }
            hm.insert(idx, new_string);
            // strings[idx] = new_string;
        }
    }

    for (k, v) in hm.into_iter() {
        strings[k] = v;
    }

    if flag {
        return strings.join("\"");
    }

    strings[0].to_owned()

    // todo!()
}
fn main() {
    // let seq = [0,1,0,3,2,3];
    // println!("{}", Solution::length_of_lis(seq.to_vec()));
    let s = string_compress(
        String::from("Hello, I will go to the \"New World Park\"."), 
        // String::from("Hello, I will go to the New World Park."), 
        vec!["hello".into(), "to".into(), "park".into()]
    );
    dbg!(s);

}

士兵走出迷宫

记忆化dfs

use std::{collections::{VecDeque, HashMap}, io::SeekFrom};
/* 
    值为1时 该位置可以走通,
    值为2时 是墙,该位置无法通过,
    值为3时 为陷阱,该位置可以走通,但是要多加三个单位的时间,
    值为4时 该位置是炸弹,可以走通 而且可以把周围相邻的四堵墙炸开。*/
// static mut BEST_PATH: Box<Vec<(usize, usize)>> = Box::new();
static mut BEST_TIME: i32 = i32::MAX;
fn dfs(
    grid: &mut Vec<Vec<i32>>, 
    i: i32, 
    j: i32, 
    mut time: i32, 
    cur_path: &mut Vec<(usize, usize)>, 
    bestpath: &mut Vec<(usize, usize)>,
    visited: &mut Vec<Vec<bool>>
) {
    let m = grid.len();
    let n = grid[0].len();
    // println!("at {} {}", i, j);

    if i < 0 || i >= n as i32 || j < 0 || j >= m as i32 {
        return;
    }

    if visited[i as usize][j as usize] {
        return;
    }
    visited[i as usize][j as usize] = true;

    if grid[i as usize][j as usize] == 2 { // 墙
        return;
    }

    if grid[i as usize][j as usize] == 3 {// 陷阱
        time += 3;
    }
    
    cur_path.push((i as usize, j as usize));
    if i as usize == n - 1 && j as usize == m - 1 {
        unsafe{
            if time < BEST_TIME {
                BEST_TIME = time;
                *bestpath = cur_path.clone();
            }
        }
        return;
    }
    
    let mut restore = Vec::new();// 事后恢复炸弹的墙壁
    let mv = [[0, 1], [1, 0], [0, -1], [-1, 0]];
    for k in 0..4 {
        let x = j + mv[k][0];
        let y = i + mv[k][1];

        if y < 0 || y >= n as i32 || x < 0 || x >= m as i32 {
            continue;
        }

        let x = x as usize;
        let y = y as usize;
        
        restore.push(
            ((x, y), grid[y][x])
        );
    }
    
    let j = j as usize;
    let i = i as usize;


    if grid[i][j] == 4 {// 炸弹
        for ((x, y), _) in restore.iter() {
            if grid[*y][*x] == 2 {
                grid[*y][*x] = 1;
            }
        }
    }

    for k in 0..4 {
        let x = j as i32 + mv[k][0];
        let y = i as i32 + mv[k][1];
        dfs(grid, y, x, time + 1, cur_path, bestpath, visited);
    }

    cur_path.pop().unwrap();
    // 恢复
    for ((x, y), r) in restore {
        grid[y][x] = r;
    }

    visited[i as usize][j as usize] = false;
}
fn main() {
    // let stdio = std::io::stdin();

    // let mut lines = String::new();
    // stdio.read_line(&mut lines).unwrap();
    // let mut lines = lines.lines();
    
    // let dime: Vec<usize> = lines.next()
    //                             .unwrap()
    //                             .split_whitespace()
    //                             .map(|x| x.parse().unwrap())
    //                             .collect();
    // let m = dime[0];
    // let n = dime[1];

    // let mut grid = Vec::new();
    // for _ in 0..m {
    //     let mut line = String::new();
    //     stdio.read_line(&mut line).unwrap();
    //     let mut lines = line.lines();

    //     let nums: Vec<u32> = lines.next()
    //                               .unwrap()
    //                               .split_whitespace()
    //                               .map(|x| x.parse().unwrap())
    //                               .collect();
    //     assert_eq!(nums.len(), n);
    //     grid.push(nums);
    // }
    // bfs(&grid, m, n);

    // ----------------------
    let mut grid = vec![
        vec![1, 2, 2, 3, 4],
        vec![1, 2, 2, 4, 2],
        vec![1, 2, 3, 1, 4],
        vec![1, 2, 3, 2, 2],
        vec![1, 1, 1, 2, 1],
    ];
    let m = 5;
    let n = 5;

    let mut cur_path = Vec::new();
    let mut bestpath = Vec::new();
    let time = 0;
    let mut visited = vec![vec![false; m]; n];
    dfs(&mut grid, 0, 0, time, &mut cur_path, &mut bestpath, &mut visited);
    // dbg!(bestpath);
    for (x, y) in bestpath {
        println!("({}, {})", x, y);
    }
}

开车到达终点的最短时长

不做hard 润!

全部评论

相关推荐

吐泡泡的咸鱼:我也工作了几年了,也陆陆续续面试过不少人,就简历来说,第一眼学历不太够,你只能靠你的实习或者论文或者项目经历,然后你没有论文,没有含金量高的比赛和奖项,只能看实习和项目,实习来说,你写的实习经历完全不清楚你想找什么工作?行研?数据分析?且写的太少了,再看项目,这些项目先不说上过大学读过研究生的都知道很水,然后对你想找的岗位有什么帮助呢?项目和实习也完全不匹配啊,你好像在努力将你所有的经历都放在简历里想表现你的优秀,但是对于你想找的岗位来说,有什么用呢?最后只能获得岗位不匹配的评价。所以你需要明白你想要找的岗位要求是什么,是做什么的,比如产品经理,然后再看你的经历里有什么匹配的上这个岗位,或者对这个岗位以及这个岗位所在的公司有价值,再写到你的简历上
点赞 评论 收藏
分享
评论
点赞
4
分享

创作者周榜

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