D82 13. Roman to Integer
Topic Link
Topic analysis
Given Roman numerals converted to Arabic numerals.
Thinking
With the substitution.
Note that the first replacement of those consecutive. For example, compared with the first replacement I
, the replacement first III
.
The final code
<?php
class Solution {
/**
* @param String $s
* @return Integer
*/
function romanToInt($s) {
$ss = str_replace(['CM','CD','XC','XL','IX','IV','M','D','C','L','X','V','I'],[',900,',',400,',',90,',',40,',',9,',',4,',',1000,',',500,',',100,',',50,',',10,',',5,',',1,'],$s);
return array_sum(array_filter(explode(',', $ss)));
}
}
复制代码
If you find this article useful, welcomed with love of power assistance.