LeetCode13 Roman to Integer Rome converted to digital

Source: Internet
Author: User

Title:

Given a Roman numeral, convert it to an integer.

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

Translation: Conversion of Rome to digital

Idea: If it is a simple Roman alphabet is better handled, but for 4,9 such, should look at its next character represents the number is not bigger than him, if the big words minus the current character represents the number. If it is small, add this number.

Like IV, represents 4. When I is read, the next character V and I should be judged, and V is larger than I, so the value of v-i should be added at this time, then the pointer skips 2.

Code Listing 1:

   public static int Romantoint (String s) {int num = 0; String roman[]={"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};int number[] = { 1000,900,500,400,100,90,50,40,10,9,5,4,1};int i = 0; int len = S.length (), while (I < s.length ()) {if (I<s.length () -1&& (C2N (S.charat (i)) <c2n (S.charat (i+1)) ))//@  1{num+=c2n (S.charat (i+1))-c2n (S.charat (i)); i+=2;} else {num +=C2N (S.charat (i)); i++;}} return num;    }    public static int c2n (char c) {switch (c) {case ' I ':  return 1;        Case ' V ':  return 5;        Case ' X ':  return ten;        Case ' L ':  return;        Case ' C ':  return;        Case ' D ':  return;        Case ' M ':  return;        Default:   throw new NumberFormatException (                "Error");}}
in the @1 place, when originally written, two judgment conditions were written in reverse. The result is that the error is crossed. I found it a while later.

Because in the && judgment, if the front is false, the back can be skipped directly, but if the previous error, then the condition will not see the direct jump error.

It's a matter of detail again. Alas

Code Listing 2:

public int Romantoint (String s) {int num = 0; String roman[]={"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};int number[] = { 1000,900,500,400,100,90,50,40,10,9,5,4,1};int i = 0;while (I<=s.length ()-1) {switch (S.charat (i)) {case ' I ': if (i <s.length () -1&& (S.charat (i+1) = = ' V ' | | S.charat (i+1) = = ' x ') num-=1;elsenum + = 1;break;case ' V ': num +=5;break;case ' x ': if (I < s.length () -1&& ( S.charat (i+1) = = ' L ' | | S.charat (i+1) = = ' c ') num-=10;elsenum+=10;break;case ' L ': num +=50;break;case ' C ': if (I < s.length () -1&& ( S.charat (i+1) = = ' D ' | | S.charat (i+1) = = ' m ') num-=100;else num+=100;break;case ' D ': Num+=500;break;case ' m ': num+=1000;break;} i++;} return num;    }
a straightforward method of integrating functions together. Looks not too brief. It is more comfortable to write the function separately.

LeetCode13 Roman to Integer Rome converted to digital

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.