# LeetCode -- Roman to Integer, Roman numerals

Source: Internet
Author: User

LeetCode -- Roman to Integer, Roman numerals

This topic is very simple. If you don't know the relationship between numbers and Roman numerals, you won't be able to start.

Question:
Principles and ideas: the Roman numerals have the following symbols:
 Basic Characters I V X L C D M Corresponding to Arabic numerals 1 5 10 50 100 500 1000

Counting rule: Second, the rule for the conversion of Roman numerals to Arabic numerals (limited to less than 3999): traverse the roman numerals from the front to the back. If a number is smaller than the previous one, add this number. Otherwise, double the previous number and add this number.

Code scheme:
`import java.util.Map;import java.util.HashMap;public class Solution {    public int romanToInt(String s) { Map<Character,Integer> map=new HashMap<Character,Integer>();map.put('I', 1);map.put('V', 5);map.put('X', 10);map.put('L', 50);map.put('C', 100);map.put('D', 500);map.put('M', 1000);        int i, total, pre, cur;          total = map.get(s.charAt(0));            for (i = 1; i < s.length(); i++) {              pre = map.get(s.charAt(i - 1));              cur = map.get(s.charAt(i));                if (cur <= pre) {                  total += cur;              } else {                  total = total - pre * 2 + cur;              }          }            return total;    }}`

Related Keywords:
Related Article

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.