[Leetcode] [13] Roman to Integer parse roman representable int type-java implementation of the constant implementation of the stack

Source: Internet
Author: User

Q:

Given a Roman numeral, convert it to an integer.

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

A:

The following solutions and code did not borrow any previous information, if there is a better solution please leave a comment in the comments section

See this question I really sigh the city will play, yesterday just solved an int to the Roman alphabet, and today in turn solution. From sighing like. The meaning of this question is to give a Roman character, convert it to an int number, and range between 1~3999. Detailed information about the Roman numerals is not copied, if you do not understand, please move int to roman numerals

The first thing we need to know is that in int, there are only one-digit 10-digit hundred thousand digits, such as 1, 3, 9. But the number of each of the Roman characters may be different, such as: I, II, III, which requires that we must find a way to distinguish between the various Roman bits. We know that it is clear that the number of x in the Roman numeral is in the range of 9~13, that is to say, if there are three characters such as X, C, M, if there is no corresponding-1 on the left (IX,XC,CM), then he represents the carry. Next we need to know that the left side of a character will only appear in the case of 4 and 9 (IV, IX, XC, etc.). So we have two kinds of ideas:

1, according to carry calculation, with yesterday resolved int to Roman character very much like.

2, according to from high to low add subtraction operation.

I chose 2, why? Because the carry in 1 is actually very complex, out of this type of IX there are XXX, and l this situation. So we choose to read the calculation from high to the character more appropriately. In the same way, we can use the method of compression stack, which is described in the application of stack.

For example, for a number 157, the Roman character is represented as CLVII. When we read the first character, we press it into the stack. Then continue to read is L, because l<c so can not be left to reduce the situation, can only be right plus, so we use the pop-up C and +l and then press into the stack is c+l=150, continue to take v<l so the same is right plus type of stack pressure stack 155, After that, the final 157.perfect is reached.

The code is as follows: (by the way, the basis of the string type is char[] so the time complexity of using the traversal and the array is the same)

< Span style= "line-height:26px" >

public class Romantointeger {public static void main (string[] args) {String Roman = "mccxxxiv"; try {System.out.println (met Hod (roman));} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} private static int method (String roman) throws Exception {//TODO auto-generated method Stubint stack = 0;int lastint = 0; for (int i =0;i<roman.length (); i++) {int thisint = Getintfromroman (Roman.charat (i)); if (thisint>lastint) {// Left-Reduced stack = stack-(lastint<<1);} stack = Stack+thisint;lastint = Thisint;} return stack;} private static int Getintfromroman (char roman) throws exception{//i (1), V (5), X (Ten), L (+), C (+), D (+), M (1000). int a = 0;switch (roman) {case ' I ': a= 1;break;case ' V ': a= 5;break;case ' X ': a= 10;break;case ' L ': a= 50;break;case ' C ': a= 10 0;break;case ' D ': a= 500;break;case ' M ': a= 1000;break;default:throw new Exception ("contains non-Roman characters");} return A;}}
Success K.O


[Leetcode] [13] Roman to Integer parse roman representable int type-java implementation of the constant implementation of the stack

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.