leetcode简单题3 N.13 罗马数字转整数 rust描述


 

// "III"  3
// "LVIII"  58
// "MCMXCIV"  1994
// 顺序遍历法
pub fn roman_to_int(s: String) -> i32 {
    let mut roman_map = std::collections::HashMap::new();
    roman_map.insert('I', 1);//哈希表
    roman_map.insert('V', 5);
    roman_map.insert('X', 10);
    roman_map.insert('L', 50);
    roman_map.insert('C', 100);
    roman_map.insert('D', 500);
    roman_map.insert('M', 1000);

    let mut result = 0;
    let chars: Vec<char> = s.chars().collect();// 使用chars方法将String转换为字符迭代器,然后收集到一个Vec<char>中:
    //let s: String = v.into_iter().collect();拓展 将vec<char>转换为String
    let len = chars.len();

    for i in 0..len { //vec的索引
        let current_value = roman_map[&chars[i]];//哈希查找希望找键的索引
        if i < len - 1 && current_value < roman_map[&chars[i + 1]] {//"LVIII" 为例 左大右小 第一个判断可以保证 i+1不会越界 减法情况就是左小右大
            result -= current_value;
        } else {
            result += current_value;
        }
    }
    result
}
// 反向遍历法
pub fn roman_to_int2(s: String) -> i32 {
    let mut result = 0;
    let mut prev_value = 0;

    for c in s.chars().rev() {
        let value = match c {//
            'I' => 1,
            'V' => 5,
            'X' => 10,
            'L' => 50,
            'C' => 100,
            'D' => 500,
            'M' => 1000,
            _ => 0,
        };

        if value < prev_value { //从右往左看,左小右大相减
            result -= value; //
        } else {
            result += value; //
        }

        prev_value = value; // 第一轮相等 且结果作为第二轮比较
    }

    result
}
// 先加后减法 都是遍历前与后,算法不同,一个遍历拆出来两个,本质还是顺序遍历法
pub fn roman_to_int3(s: String) -> i32 {
    let mut roman_map = std::collections::HashMap::new();
    roman_map.insert('I', 1);
    roman_map.insert('V', 5);
    roman_map.insert('X', 10);
    roman_map.insert('L', 50);
    roman_map.insert('C', 100);
    roman_map.insert('D', 500);
    roman_map.insert('M', 1000);

    let mut result = 0;
    let chars: Vec<char> = s.chars().collect();

    for &c in &chars { //&为模式匹配
        result += roman_map[&c];// get方法和[]似乎都需要引用 全加
    }

    for i in 0..chars.len() - 1 { //遍历下标-1 因为要和i+1比 防止出界
        if roman_map[&chars[i]] < roman_map[&chars[i + 1]] { //
            result -= 2 * roman_map[&chars[i]];//因为已经加了一次所以要减去二倍
        }
    }
    result
}
// 特殊组合替换法 因为只有六种减法情况
pub fn roman_to_int4(s: String) -> i32 {
    let mut roman_map = std::collections::HashMap::new();
    roman_map.insert('I', 1);
    roman_map.insert('V', 5);
    roman_map.insert('X', 10);
    roman_map.insert('L', 50);
    roman_map.insert('C', 100);
    roman_map.insert('D', 500);
    roman_map.insert('M', 1000);
    roman_map.insert('a', 4);   // IV
    roman_map.insert('b', 9);   // IX
    roman_map.insert('c', 40);  // XL
    roman_map.insert('d', 90);  // XC
    roman_map.insert('e', 400); // CD
    roman_map.insert('f', 900); // CM

    let mut modified_s = s;//替换后
    modified_s = modified_s.replace("IV", "a");
    modified_s = modified_s.replace("IX", "b");
    modified_s = modified_s.replace("XL", "c");
    modified_s = modified_s.replace("XC", "d");
    modified_s = modified_s.replace("CD", "e");
    modified_s = modified_s.replace("CM", "f");

    let mut result = 0;
    for c in modified_s.chars() {
        result += roman_map[&c];//
    }
    result
}
fn main() {
    assert_eq!(roman_to_int(String::from("III")), 3);
    assert_eq!(roman_to_int(String::from("LVIII")), 58);
    assert_eq!(roman_to_int(String::from("MCMXCIV")), 1994);

    assert_eq!(roman_to_int2(String::from("III")), 3);
    assert_eq!(roman_to_int2(String::from("LVIII")), 58);
    assert_eq!(roman_to_int2(String::from("MCMXCIV")), 1994);

    assert_eq!(roman_to_int3(String::from("III")), 3);
    assert_eq!(roman_to_int3(String::from("LVIII")), 58);
    assert_eq!(roman_to_int3(String::from("MCMXCIV")), 1994);

    assert_eq!(roman_to_int4(String::from("III")), 3);
    assert_eq!(roman_to_int4(String::from("LVIII")), 58);
    assert_eq!(roman_to_int4(String::from("MCMXCIV")), 1994);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值