#13 Roman to Integer
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int getIndex(char roman[], char x){
int i = 0;
int index = 0;
int len = strlen(roman);
while(i < len){
if(x == roman[i]){
index = i;
break;
}
i++;
}
return index;
}
int romanToInt(char* s) {
int i = 1;
int index1 = 0;
int index2 = 0;
int intdata = 0;
int charlen = strlen(s);
char roman[] = {'I', 'V', 'X', 'L', 'C', 'D', 'M' };
int data[] = {1, 5, 10, 50, 100, 500, 1000};
index1 = getIndex(roman, s[0]);
intdata += data[index1];
while(i < charlen){
index2 = getIndex(roman, s[i]);
if(index2 <= index1)
intdata += data[index2];
else if(index2 > index1)
intdata += data[index2] - 2 * data[index1];
index1 = index2;
i++;
}
return intdata;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* longestCommonPrefix(char** strs, int strsSize) {
int i, j, min, len;
char *ret;
char x;
if(strsSize == 0) return "";
if(strsSize == 1) return strs[0];
min = strlen(strs[0]);
for(i = 0; i < strsSize; i++){
len = strlen(strs[i]);
if(len < min)
min = len;
}
len = 0;
ret = (char*)malloc(min + 1);
for(i = 0; i < min; i++){
x = strs[0][i];
for(j = 1; j < strsSize; j++){
if(strs[j][i] != x){
break;
}
}
if(j < strsSize) break;
else {
ret[len] = x;
len++;
}
}
ret[len] = '\0';
return ret;
}