弱智题目,只能怪自己rust水平不行。
哎,记住要移除掉最后一个元素不要用remove这么傻逼的方式了。直接pop掉。
就是把给的数组里的元素一个个push进另一个就行了,如果遇到相对方向的,把新数据的back pop掉。

#![feature(uniform_paths)]

#[derive(Debug, PartialEq,Clone,Copy)]
enum Direction { NORTH, SOUTH, EAST, WEST }


fn dir_reduc(arr: &[Direction]) -> Vec<Direction> {
    use Direction::*;
    let mut ret: Vec<Direction> = Vec::new();
    for i in 0..arr.len(){
        if ret.is_empty() { ret.push(arr[i]);}
        else{
           match (arr[i],*ret.last().unwrap()) {
               (NORTH,SOUTH)|(SOUTH,NORTH)|(WEST,EAST)|(EAST,WEST) =>{ ret.pop();}
               _ => { ret.push(arr[i]);}
           }
        }
    }
    println!("{:#?}",ret);
    ret
}

fn main() {
    use Direction::*;
    let a = [NORTH, SOUTH, SOUTH, EAST, WEST, NORTH, WEST];
    dir_reduc(&a);
    println!("Hello, world!");
}

#[test]
fn returns_expected() {
    use Direction::*;
    let a = [NORTH, SOUTH, SOUTH, EAST, WEST, NORTH, WEST];
    assert_eq!(dir_reduc(&a), [WEST]);
    let a = [NORTH, WEST, SOUTH, EAST];
    assert_eq!(dir_reduc(&a), [NORTH, WEST, SOUTH, EAST]);
}

标签: none

添加新评论