Earlier I mentioned we need extra space. We need that extra space to handle these 6 conditions, IV, IX, XL, XC, CD, CM. The data structure we need for this problem is Dictionary because we need to store Key-Value pairs. The first one with O(n*m) complexity and second one with O(n). Given an integer, convert it to a roman numeral. C can be placed before D (500) and M (1000) to make 400 and 900.X can be placed before L (50) and C (100) to make 40 and 90. I can be placed before V (5) and X (10) to make 4 and 9.There are six instances where subtraction is used, The same principle applies to the number nine, which is written as IX. Because the one is before the five we subtract it making four. Instead, the number four is written as IV. However, the numeral for four is not IIII. Roman numerals are usually written largest to smallest from left to right. The number 27 is written as XXVII, which is XX + V + II. 12 is written as XII, which is simply X + II. Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M.įor example, 2 is written as II in Roman numerals, just two one's added together. Here is the problem straight out of Leetcode: What do I mean by extra space? Well, let's see. This problem is almost similar with a little extra space. What we did last time is to compare current and next roman characters and perform basic addition and subtraction. This is leetcode's problem number 12 with difficulty level set to medium. In the last article, we saw how to convert Roman numbers to numeric values. In this article we will do exactly the opposite.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |