题目地址:https://leetcode-cn.com/problems/roman-to-integer/
思路:字符字典。这算法耗时太长。以后再想其它的。
class Solution {
/**
* @param String $s
* @return Integer
*/
function romanToInt($s) {
$search = [
'IV',
'IX',
'XL',
'XC',
'CD',
'CM',
'I',
'V',
'X',
'L',
'C',
'D',
'M',
];
$replace = [
'4+',
'9+',
'40+',
'90+',
'400+',
'900+',
'1+',
'5+',
'10+',
'50+',
'100+',
'500+',
'1000+',
];
$s = rtrim(str_replace($search, $replace, $s),'+');
return eval('return '.$s.';');
}
}