Roman to integer--Roman Digital Transformation algorithm

Source: Internet
Author: User

This article is in the study summary, welcome reprint but please specify Source:http://blog.csdn.net/pistolove/article/details/41486885

The knowledge you may have learned from this article is as follows:

(1) Understand the problem solving ideas, in the future similar scenario, if not think of a better method, you can consider the method of this article, although the efficiency is not particularly high.

(2) Ability to learn about the interception and hashmap of strings.


Roman to Integer

Given a Roman numeral, convert it to an integer.

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

The idea of solving problems is as follows:

(1) This problem is not difficult in fact, you can use the switch to determine the condition, and based on the return value accumulation to obtain results. This article uses the HashMap.

(2) The Roman numerals and the corresponding integers are stored in the HashMap. Because the subject limit is 1-3999, the Roman numbers that need to be stored are: 1-9, 10-100, 100-1000, 1000-3000, the number actually

Not much.

(3) for a given Roman numeral string, first, its length is judged, if the length is not 0, then continue. Second, we find that the Roman numerals have an inclusive relationship between numbers, such as III containing II and

I, therefore, for a given Roman numeral string, you need to determine whether its substring corresponds to a value in the map is empty. We first intercept the first character, determine if its value in the map is empty, and if it is not empty, continue to intercept

Take the second character, if the two characters in the map value is not empty, we continue to intercept to the third character, if the three characters in the map value is not empty, continue ..., until the captured characters in the map

The expected value is null, then the last added character corresponding to the value in the map is stored, and so on, until all characters in the string are involved in the interception operation, the resulting value is the corresponding integer value.



The algorithm implementation code is as follows (PS: I have limited technology, at present, can not write efficient algorithms, we have a good algorithm to share, thank you)

public int romantoint (String s) {map<string, integer> maps = new hashmap<string, Integer> (); Maps.put ("I", 1), Maps.put ("II", 2), Maps.put ("III", 3), Maps.put ("IV", 4), Maps.put ("V", 5), Maps.put ("VI ", 6); Maps.put (" VII ", 7); Maps.put (" VIII ", 8), Maps.put (" IX ", 9); Maps.put (" X "," ten "); Maps.put (" XX "); Maps.put (" XXX ", Maps.put ("XL", +), Maps.put ("L", "a"), Maps.put ("LX"), Maps.put ("LXX"), Maps.put ("LXXX"), Maps.put ("XC", ("C", "Maps.put"), Maps.put ("CC", "$"), Maps.put ("CCC"), Maps.put ("CD", "n"), Maps.put ("D", +), Maps.put ("DC Maps.put ("DCC", Maps.put ("DCCC"), Maps.put ("CM", "n"), Maps.put ("M", "n"), Maps.put ("MM", 2000); Maps.put ("MMM", +); if (s.length () = = 0) return-1;int count = 0;int flag = 0;for (int i = 0; i < s.length (); i++) {WH Ile (Flag < S.length () && maps.get (s.substring (i, flag + 1))! = null) {flag++;} Count = Count + maps.get (s.substring (i, flag)); i = flag-1;} return count;} 


Roman to integer--Roman Digital Transformation algorithm

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.