"Leetcode" 13. Roman to Integer

Source: Internet
Author: User

Title Description:

Given a Roman numeral, convert it to an integer.

Problem Solving Analysis:

This problem as long as Baidu change the rules, and then this explanation to write code can. There is no difficulty in achieving it, just look at the code

Specific code:

1  Public classSolution {2       Public  intRomantoint (String s) {3          int[] value={1000,500,100,50,10,5,1};4          Char[] array = "MDCLXVI". ToCharArray ();5 6          intSum=0;7           for(intI=0;i<s.length (); i++){8              CharCh=S.charat (i);9              intindex=findindex (array, ch);Ten              if(index==2| | index==4| | Index==6){ One                  if(I+1<s.length () && s.charat (i+1) = = Array[index-1]){ Asum=sum+value[index-1]-Value[index]; -i++; -                  } the                  Else if(I+1<s.length () && s.charat (i+1) = = Array[index-2]){ -sum=sum+value[index-2]-Value[index]; -i++; -                  } +                  Else{ -sum+=Value[index]; +                  } A              } at              Else{ -sum+=Value[index]; -              } -          } -          returnsum; -      } in       Public Static intFindIndex (Char[] Array,Charch) { -           for(inti=0;i<array.length;i++){ to              if(array[i]==ch) +                  returni; -          } the          return-1; *      } $}

"Leetcode" 13. 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.