File: root - text - article - 2020 - 01 - convert-roman-numerals-to-decimal.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 486 Views, 28660 Search Bots | 160 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 486 Views, 28660 Search Bots | 160 Words
| Browse | Archive
Hi, here's your problem today. This problem was recently asked by Twitter:
Given a Roman numeral, find the corresponding decimal value. Inputs will be between 1 and 3999.
Example:
Input: IX
Output: 9
Input: VII
Output: 7
Input: MCMIV
Output: 1904
Roman numerals are based on the following symbols:
Numbers are strings of these symbols in descending order. In some cases, subtractive notation is used to avoid repeated characters. The rules are as follows:
1.) I placed before V or X is one less, so 4 = IV (one less than 5), and 9 is IX (one less than 10)
2.) X placed before L or C indicates ten less, so 40 is XL (10 less than 50) and 90 is XC (10 less than 100).
3.) C placed before D or M indicates 100 less, so 400 is CD (100 less than 500), and 900 is CM (100 less than 1000).
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 486 Views, 28660 Search Bots | 160 Words Given a Roman numeral, find the corresponding decimal value. Inputs will be between 1 and 3999.
Example:
Input: IX
Output: 9
Input: VII
Output: 7
Input: MCMIV
Output: 1904
Roman numerals are based on the following symbols:
I 1
IV 4
V 5
IX 9
X 10
XL 40
L 50
XC 90
C 100
CD 400
D 500
CM 900
M 1000
Numbers are strings of these symbols in descending order. In some cases, subtractive notation is used to avoid repeated characters. The rules are as follows:
1.) I placed before V or X is one less, so 4 = IV (one less than 5), and 9 is IX (one less than 10)
2.) X placed before L or C indicates ten less, so 40 is XL (10 less than 50) and 90 is XC (10 less than 100).
3.) C placed before D or M indicates 100 less, so 400 is CD (100 less than 500), and 900 is CM (100 less than 1000).
class Solution():
def romanToInt(self, s):
# Fill this in.
n = 'MCMX'
print(Solution().romanToInt(n))
# 1910
Related Articles
- Daily Interview Problem: Get all Values at a Certain Height in a Binary Tree
- Algorithm Interview: Permutations of numbers
- Daily Interview Problem: Given two arrays, write a function to compute their intersection.
- Reverse a Directed Graph
- Daily Interview Problem:Create a balanced binary search tree
- Fix Brackets
- First and Last Indices of an Element in a Sorted Array
- Making a Height Balanced Binary Search Tree
- Daily Interview Problem: Arithmetic Binary Tree
- Non-decreasing Array with Single Modification
©2006~2024 SteakOverCooked - 0.00775 Seconds(s) - 2707.129 KB/s - 71 Online Memory: 495.84 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="index, follow">
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !