leetcode字符串处理经典



Given a roman numeral, convert it to an integer.

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


int romanToInt(char* s) {
    

	int length = strlen(s);
	int i;
	int a[26];
	a['M' - 'A'] = 1000;
	a['D' - 'A'] = 500;
	a['C' - 'A'] = 100;
	a['L' - 'A'] = 50;
	a['X' - 'A'] = 10;
	a['V' - 'A'] = 5;
	a['I' - 'A'] = 1;
	
    int p ,	q = 0;;
    int num = 0;
	
	for( i = length -1 ; i >= 0 ; i--)
    {  
        p = a[s[i] - 'A'];
        num += ( p >= q )?p:-p;
        q = p;
    }
   
    return num;
    
}

你可能感兴趣的:(leetcode)