#LeetCode

2 messages · Page 1 of 1 (latest)

fleet thicket
#

found one!

#
use std::collections::HashMap;


impl Solution {
    #[inline]
      pub fn roman_to_int(s: String) -> i32 {
        let hash = HashMap::from([
            ('N', 0),
            ('I', 1),
            ('V', 5),
            ('X', 10),
            ('L', 50),
            ('C', 100),
            ('D', 500),
            ('M', 1000),
        ]);

        let mut sp = s.chars().peekable();
        let mut res = 0;
        while let Some(current) = sp.next() {
            println!("{current}");
            let next = &sp.peek().unwrap_or(&&'N');
            let (current, next) = (hash.get(&current).unwrap(), hash.get(next).unwrap());
            if current >= next {
                res += current;
            } else {
                res += next - current;
                sp.next();
            }
        }
        res
    }
}