
// "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);
}

被折叠的 条评论
为什么被折叠?



