Leetcode Roman to integer

Source: Internet
Author: User

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.



The idea is quite simple. The key is to understand the rules of the Roman numerals:



There are a total of 7 Ma data words, namely I (1), V (5), x (10), L (50), C (100), D (500) and M (1000 ). Any positive number can be expressed according to the following rules. It should be noted that there is no "0" in the Roman numerals and it is irrelevant to the hexadecimal notation. Generally, it is assumed that only the mathematical words are used for data record, rather than calculation.

  • Repeated data: When a digital keyword is repeated, it indicates a multiple times of the number.
  • Right and left outer:
    • On the right hand side of the large digital word, I wrote a small digital word, indicating that the big number and the small number are added.
    • On the left side of a large digital word, a small digital word is displayed, indicating that a large number is reduced.
    • There is a limit on the number to be subtracted from the left, which is limited to I, X, and C. For example, 45 cannot be written as VL, but XLV
    • However, the left digit cannot span one digit. For example, 99 cannot be represented by IC (), but xcix. (Equivalent to Arabic numerals, each digit is represented separately .)
    • The left digit must be one digit, for example, 8 digits must be VIII, not iix.
    • The right-side number cannot exceed three characters, for example, 14 characters can be converted into XIV, rather than xiiii. (See "Data limit" below .)
  • Add a shard to the nearest shard:
    • Add a comment or a lower mark to the top of the Trojan data ?, This number is multiplied by 1000, Which is 1000 times the original number.
    • Similarly, if there are two rows above, that is, 1000000 () times the original number.
  • Data limit:
    • The same data volume can only appear three times at most, for example, 40 cannot be expressed as XXXX, but XL.
    • Exception: Because IV is the first character in the word of Zhu Pite (ivpiter, which does not contain J and U), iiii is sometimes used to replace IV.


For details, see the notes:


import java.util.*;public class solution { public static int romanToInt(String s) { Map<String,Integer> roman = new Hashtable<String,Integer>(); roman.put("I", 1); roman.put("V", 5); roman.put("X", 10); roman.put("L", 50); roman.put("C", 100); roman.put("D", 500); roman.put("M", 1000); int res = 0; String temp = ""; if(s.length()==1) return roman.get(s.charAt(0)+temp);  for(int i=s.length()-1;i>=0;i--) { if(i==s.length()-1) { res = roman.get(s.charAt(i)+temp); continue; }  if(roman.get(s.charAt(i)+temp)>roman.get(s.charAt(i+1)+temp)) { res+=roman.get(s.charAt(i)+temp); } else res-=roman.get(s.charAt(i)+temp); } return res; }    public static void main(String[] args){    int t =romanToInt("MCMXCVI");    System.out.println(t);    }}


Leetcode Roman to integer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.