Leetcode#12 integer-to-roman
https://leetcode.com/problems/integer-to-roman/submissions/ * 12.php <?php class Solution { // a[0] > a[1] > a[2] ... private static function indexLessThan($a, $ans, $start=0) { $n = count($a); for ($i = $start; $i < $n; $i++) { if ($a[$i] <= $ans) { return $i; } } return -1; } /** * @param Integer $num * @return String */ public function intToRoman($num) { $a = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; $b = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L','XL','X','IX','V', 'IV', 'I']; $r = []; while (($i = self::indexLessThan($a, $num, 0)) >= 0) { $num -= $a[$i]; array_push($r, $b[$i]); }