天道酬勤,学无止境

罗马

Leetcode 13. Roman to Integer C语言

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.罗马数字与整数互转int getInt(char c){ int temp; switch(c){ case 'I':return 1; case 'V':return 5; case 'X':return 10; case 'L':return 50; case 'C':return 100; case 'D':return 500; case 'M':return 1000; } return 0; } int romanToInt(char* s) { int temp; int sum=getInt(s[0]); for(int i=1;i<strlen(s);i++){ if(getInt(s[i-1])<getInt(s[i])){ sum+=getInt(s[i]); sum-=2*getInt(s[i-1]); // printf("%d-->%d\n",getInt(s[i-1]),getInt(s[i])); }else{ sum+=getInt(s[i]); // printf("{%d}",getInt(s[i])); } } return sum

2021-03-27 02:21:05    分类:博客    数字   罗马   转整数   leetcode