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