amarao (amarao_san) wrote,
amarao
amarao_san

продолжая leetcode

Первый раз я ощутил Силу. Задача - roman numerals, с обещанием, что на входе нет мусора.

pub fn roman_to_int(s: String) -> i32 {
    let mut acc = 0;
    let mut stream = s.chars().peekable();
    while let Some(curr) = stream.next(){
        let next = stream.peek();
        acc += match (curr, next){
            ('I', Some('V')) => -1,
            ('I', Some('X')) => -1,
            ('X', Some('L')) => -10,
            ('X', Some('C')) => -10,
            ('C', Some('D')) => -100,
            ('C', Some('M')) => -100,
            ('I', _) => 1,
            ('V', _) => 5,
            ('X', _) => 10,
            ('L', _) => 50,
            ('C', _) => 100,
            ('D', _) => 500,
            ('M', _) => 1000,
            _ => panic!("Bad symbol")
        }
    }
    acc
}

Результат, как всегда, 0ms, 2МБ памяти.

PS Обещают, что скоро можно будет писать так:

('I', Some('V'|'X')) => -1,
Tags: rust
Subscribe

  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 18 comments