Home > Article > Backend Development > Leetcode PHP题解--D82 13. Roman to Integer
D82 13. Roman to Integer
Question link
Question Analysis
Convert the given Roman numerals to Arabic numerals.
Ideas
Use substitution method.
Be careful to replace those that appear consecutively first. For example, rather than replacing I first, replace III first. (php video tutorial)
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))); } }
The above is the detailed content of Leetcode PHP题解--D82 13. Roman to Integer. For more information, please follow other related articles on the PHP Chinese website!