Conversion of integer data to Roman numerals: If that
INPUT: III
Output:. 3
input:IV
output:4
input:IX
output:9
input:LVIII
output:58
input:MCMXCIV
output:1994
int romanToInt(string s) {
int num = 0;
vector<int> vec;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == 'I') {
vec.push_back(1);
continue;
}
if (s[i] == 'V') {
vec.push_back(5);
continue;
}
if (s[i] == 'X') {
vec.push_back(10);
continue;
}
if (s[i] == 'L') {
vec.push_back(50);
continue;
}
if (s[i] == 'C') {
vec.push_back(100);
continue;
}
if (s[i] == 'D') {
vec.push_back(500);
continue;
}
if (s[i] == 'M') {
vec.push_back(1000);
continue;
}
}
for (int i = 0; i < s.size() - 1; ++i) {
int j = i + 1;
if (vec[i] < vec[j] && vec[j] / vec[i] <= 10) {
vec[i] = vec[j] - vec[i];
vec[j] = 0;
i++;
}
}
for (auto i : vec)
num += i;
return num;
}
A first loop for all integer values corresponding to the Roman numerals inputted to the vector, there is no second cycle is determined as (IV) which indicates (VI), the number, if it is replaced, a range for the last cycle the number of containers all together return, complexity is O (N)