"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)"