您的位置:首页 > 其它

Leetcode 13

2016-03-31 14:38 225 查看
Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

class Solution {
public:
int romanToInt(string s) {
int m=0,i=0,j=0,k=0;
while (s[m]!='\0')
m++;
// int *w=new int[];
int w[m];

while(s[i]!='\0')
{
if(s[i]=='I')
w[i]=1;
else if(s[i]=='V')
w[i]=5;
else if(s[i]=='X')
w[i]=10;
else if(s[i]=='L')
w[i]=50;
else if(s[i]=='C')
w[i]=100;
else if(s[i]=='D')
w[i]=500;
else if(s[i]=='M')
w[i]=1000;
i++;
}

j=i-1;
k=w[j];
while(j>0)
{
if(w[j]>w[j-1])
k=k-w[j-1];
else
k=w[j-1]+k;
j--;
}
// delete []w;
return k;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: