leetcode roman to integer
注意数组定义在leetcode里面必须指定长度。
class Solution { public: char a[8]="MDCLXVI"; int b[7]={1000,500,100,50,10,5,1}; int romanToInt(string s) { int k=0; int j=0; int i=0; for(;i<=6;i++) { if(i%2==0) { if(s[j]==a[i]) { j++;k+=b[i]; } if(s[j]==a[i]) { j++;k+=b[i]; if(s[j]==a[i]) { j++;k+=b[i]; } } else if(s[j]==a[i-2]&&i>0) { j++;k+=8*b[i]; } else if(s[j]==a[i-1]&&i>0) { j++;k+=3*b[i]; } } if(i%2!=0) { if(s[j]==a[i]) { j++;k+=b[i]; } while(s[j]==a[i+1]) { j++;k+=b[i+1]; } } } return k; } };