GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode-3/python/012_Integer_to_Roman.py at master · Xutete/leetcode-3 Expand file tree/ Copy path012_Integer_to_Roman.pyMore file actions 47 lines (45 loc) · 1.49 KB / Copy path012_Integer_to_Roman.pyFile metadata and controls47 lines (45 loc) · 1.49 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 # class Solution(object): # def intToRoman(self, num: int) -> str: # """ # :type num: int # :rtype: str # """ # class Solution(object): # def intToRoman(self, num: int) -> str: ## http://www.rapidtables.com/convert/number/how-number-to-roman-numerals.htm # roman_dim = [[1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], # [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], # [9,'IX'], [5, 'V'], [4, 'IV'], [1, 'I']] # if num == 0: # return '' # roman_str = '' # current, dim = num, 0 # while current != 0: # while current // roman_dim[dim][0] == 0: # dim += 1 # while current - roman_dim[dim][0] >= 0: # current -= roman_dim[dim][0] # roman_str += roman_dim[dim][1] # return roman_str def intToRoman(self, num: int) -> str: values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"] roman = '' i = 0 while num > 0: k = num // values[i] for j in range(k): roman += symbols[i] num -= values[i] i += 1 return roman if __name__ == '__main__': # begin s = Solution() print s.intToRoman(90) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |