题目难度:简单
题目地址:
https://leetcode-cn.com/problems/roman-to-integer/
// 提交答案
int romanToInt(char * s){
int getNum[26];
getNum['I'-'A'] = 1;
getNum['V'-'A'] = 5;
getNum['X'-'A'] = 10;
getNum['L'-'A'] = 50;
getNum['C'-'A'] = 100;
getNum['D'-'A'] = 500;
getNum['M'-'A'] = 1000;
int ans = 0;
int len = strlen(s);
for(int i=0;i<len;++i){
int value = getNum[s[i]-'A'];
if(i<len-1 && value < getNum[s[i+1]-'A']){
ans -= value;
}else{
ans += value;
}
}
return ans;
}
// 这是哈希表练习,今天看了哈希表的相关知识点
哦豁,这三天就过去了!