"Leetcode-Interview algorithm classic-java Implementation" "013-roman to Integer (roman numerals into integers)"

Source: Internet
Author: User

"013-roman to Integer (roman numeral turns into integer)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.

Main Topic

Given a Roman number, convert it to the corresponding integer.
The number entered is between 1-3999.

Thinking of solving problems

The addition is performed according to the Roman numeral and integer number correspondence, if the previous number is subtracted from the latter, otherwise it is added.

Code Implementation
 Public classSolution { Public int Romantoint(String s) {intresult =0;intPrev =0;//Record the value of the previous number         for(inti = s.length ()-1; I >-1; i--) {Switch(S.charat (i)) { Case ' I '://1                    if(1< prev) {result-=1; }Else{result + =1; } prev =1; Break; Case ' V '://5                    if(5< prev) {result-=5; }Else{result + =5; } prev =5; Break; Case ' X '://Ten                    if(Ten< prev) {result-=Ten; }Else{result + =Ten; } prev =Ten; Break; Case ' L '://                    if( -< prev) {result-= -; }Else{result + = -; } prev = -; Break; Case ' C '://                    if( -< prev) {result-= -; }Else{result + = -; } prev = -; Break; Case ' D '://                    if( -< prev) {result-= -; }Else{result + = -; } prev = -; Break; Case ' M ':// +Result + = +; Prev = +; Break; }        }returnResult }
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/46963377"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "013-roman to Integer (roman numerals into integers)"

Related Article

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.