【leetcode】罗马数字转整数

 

int romanToInt(char * s){
    int* hash = (int*)calloc(26,sizeof(int));
    hash['I'-65] = 1;
    hash['V'-65] = 5;
    hash['X'-65] = 10;
    hash['L'-65] = 50;
    hash['C'-65] = 100;
    hash['D'-65] = 500;
    hash['M'-65] = 1000;
    int sum=0;
    for (int i=0; i<strlen(s)-1; i++)
    {
        sum+=(hash[s[i]-65] >= hash[s[i+1]-65])?hash[s[i]-65]:-hash[s[i]-65];
    }
    sum+=hash[s[strlen(s)-1]-65];
    return sum;
}

 

posted @ 2020-09-07 12:04  温暖了寂寞  阅读(104)  评论(0)    收藏  举报