2020年3月30日 星期一

[LeetCode] Medium - 12. Integer to Roman (G,M,A,F)

Source From Here
Question
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
  1. Symbol       Value  
  2. I             1  
  3. V             5  
  4. X             10  
  5. L             50  
  6. C             100  
  7. D             500  
  8. M             1000  
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
* I can be placed before V (5) and X (10) to make 4 and 9.
* X can be placed before L (50) and C (100) to make 40 and 90.
* C can be placed before D (500) and M (1000) to make 400 and 900.
* Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.


Example 1:
Input: 3
Output: "III"

Example 2:
Input: 4
Output: "IV"

Example 3:
Input: 9
Output: "IX"

Example 4:
Input: 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.

Example 5:
Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Solution

Naive
  1. class Solution:  
  2.     def intToRoman(self, num: int) -> str:  
  3.         def i2r(d, roman_digits=['I', 'V', 'X']):  
  4.             if d < 4:  
  5.                 return roman_digits[0] * d  
  6.             elif d == 4:  
  7.                 return roman_digits[0] + roman_digits[1]  
  8.             elif d == 5:  
  9.                 return roman_digits[1]  
  10.             elif d <= 8:  
  11.                 return roman_digits[1] + roman_digits[0] * (d - 5)  
  12.             else:  
  13.                 return roman_digits[0] + roman_digits[2]  
  14.               
  15.               
  16.         roman_level_digits = [['I', 'V', 'X'], ['X', 'L', 'C'], ['C', 'D', 'M'], ['M', '-', '-']]  
  17.         roman_num = ''  
  18.         level = 0  
  19.         while num > 0:  
  20.             remainder = num % 10  
  21.             roman_num = i2r(remainder, roman_level_digits[level]) + roman_num  
  22.             num //= 10  
  23.             level += 1  
  24.               
  25.         return roman_num  
Runtime: 44 ms, faster than 82.16% of Python3 online submissions for Integer to Roman.
Memory Usage: 13.9 MB, less than 6.15% of Python3 online submissions for Integer to Roman.

Golden (link)
  1. def intToRoman(self, num: int) -> str:  
  2.     thousands = ["", "M", "MM", "MMM"]  
  3.     hundreds = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]  
  4.     tens = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]  
  5.     ones = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]  
  6.     return thousands[num // 1000] + hundreds[num % 1000 // 100] + tens[num % 100 // 10] + ones[num % 10]  
Runtime: 44 ms, faster than 82.16% of Python3 online submissions for Integer to Roman.
Memory Usage: 13 MB, less than 98.46% of Python3 online submissions for Integer to Roman.


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...