將給定的羅馬數字轉換成阿拉伯數字。spa
用替換法。.net
要注意,先替換連續出現的那些。例如,比先替換I
,要先替換III
。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)));
}
}
複製代碼
若以爲本文章對你有用,歡迎用愛發電資助。leetcode