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