题目难度:简单
题目地址:
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;
}
// 这是哈希表练习,今天看了哈希表的相关知识点

哦豁,这三天就过去了!

posted on 2022-03-18 22:40  北冥有鱼其名为嘉  阅读(20)  评论(0编辑  收藏  举报