Problem description
Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
Algorithm
Code One:
1 Public intRomantoint (String s) {2 intRET = 0;3 Charc, C1;4 for(inti = S.length ()-1; I >= 0; i--) {5 if(i = = S.length ()-1) {6c =S.charat (i);7 if(c = = ' I ') {8RET + = 1;9}Else if(c = = ' V ') {TenRET + = 5; One}Else if(c = = ' X ') { ARET + = 10; -}Else if(c = = ' L ')) { -RET + = 50; the}Else if(c = = ' C ') { -RET + = 100; -}Else if(c = = ' D ') { -RET + = 500; +}Else if(c = = ' M ') { -RET + = 1000; + } A Continue; at } - -c =S.charat (i); -C1 = S.charat (i + 1); - if(c = = ' I ') { - if(C1 = = ' V ' | | c1 = = ' X ') { inRET-= 1; -}Else { toRET + = 1; + } -}Else if(c = = ' V ') { theRET + = 5; *}Else if(c = = ' X ') { $ if(C1 = = ' L ' | | c1 = = ' C ') {Panax NotoginsengRET-= 10; -}Else { theRET + = 10; + } A}Else if(c = = ' L ')) { the +RET + = 50; - $}Else if(c = = ' C ') { $ if(C1 = = ' D ' | | c1 = = ' M ') { -RET-= 100; -}Else { theRET + = 100; - }Wuyi}Else if(c = = ' D ') { the -RET + = 500; Wu -}Else if(c = = ' M ') { AboutRET + = 1000; $ } - } - - returnret; A}
Code two:
1 Public intRomantoint (String s) {2Hashtable<character,integer>value;3Value=NewHashtable<character,integer>();4Value.put (' M ', 1000);5Value.put (' D ', 500);6Value.put (' C ', 100);7Value.put (' L ', 50);8Value.put (' X ', 10);9Value.put (' V ', 5);TenValue.put (' I ', 1); One intSum=0; A for(intI=0;i<s.length (); i++) - { - if(I<s.length () -1&&value.get (S.charat (i)) <value.get (S.charat (i+1))) thesum-=Value.get (S.charat (i)); - Else -sum+=Value.get (S.charat (i)); - } + returnsum; -}
Code Three:
1 Public intromantoint (String str) {2 int[] A =New int[26];3a[' I '-' A '] = 1;4a[' V '-' A '] = 5;5a[' X '-' A '] = 10;6a[' L '-' A '] = 50;7a[' C '-' A '] = 100;8a[' D '-' A '] = 500;9a[' M '-' A '] = 1000;Ten Charprev = ' A '; One intsum = 0; A for(CharS:str.tochararray ()) { - if(A[s-' a '] > A[prev-' a ']) { -sum = sum-2 * A[prev-' a ']; the } -sum = sum + a[s-' a ']; -Prev =s; - } + returnsum; -}
Code four:
1 Public intRomantoint (String s) {2 intnums[]=New int[S.length ()];3 for(intI=0;i<s.length (); i++){4 Switch(S.charat (i)) {5 CaseM:6nums[i]=1000;7 Break;8 CaseD:9nums[i]=500;Ten Break; One CaseC: Anums[i]=100; - Break; - CaseL: theNums[i]=50; - Break; - CaseX : -nums[i]=10; + Break; - CaseV: +Nums[i]=5; A Break; at Case' I ': -Nums[i]=1; - Break; - } - } - intSum=0; in for(inti=0;i<nums.length-1;i++){ - if(nums[i]<nums[i+1]) tosum-=Nums[i]; + Else -sum+=Nums[i]; the } * returnSum+nums[nums.length-1]; $}
Precautions
1. There are seven Roman numerals, namely I (1), V (5), X (Ten), L (+), C (+), D (+), M (1000). Any positive integer can be represented by the following rules.
Repeat several times: a Roman number repeats several times, indicating several times the number.
Right plus left minus: a smaller Roman number on the right side of a larger Roman numeral, indicating a large number plus a small number. A smaller Roman number on the left side of a larger number indicates a large number to reduce the number. However, the left subtraction cannot span the hierarchy. For example, 99 can not be used to express the IC, with XCIX.
Add line multiply: Add a dash above a Roman numeral or write m at the bottom right, indicating that this number is multiplied by 1000, which is 1000 times times the original number. Similarly, if there are two horizontal lines above, that is 1 million times times the original number.
Unit limit: The same unit can only appear 3 times, such as 40 can not be expressed as XXXX, but to be represented as XL
2. For string s= "Qwert", S[0] is q instead of T.
Roman to Integer