Mister-Hope
Mister-HopeC++JavaScriptTypeScript
C++
/*
* Runtime: 24 ms, faster than 22.26% of C++ online submissions for Integer to
* Roman.
*
* Memory Usage: 12.6 MB, less than 11.00% of C++ online submissions for Integer
* to Roman.
*/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
string intToRoman(int num) {
if (num <= 0) return "";
vector<string> thousands{"", "M", "MM", "MMM"};
vector<string> hundreds{"", "C", "CC", "CCC", "CD",
"D", "DC", "DCC", "DCCC", "CM"};
vector<string> tens{"", "X", "XX", "XXX", "XL",
"L", "LX", "LXX", "LXXX", "XC"};
vector<string> units{"", "I", "II", "III", "IV",
"V", "VI", "VII", "VIII", "IX"};
return thousands[num / 1000] + hundreds[(num % 1000) / 100] +
tens[(num % 100) / 10] + units[num % 10];
}
};
1
/*
* Runtime: 4 ms, faster than 93.51% of C++ online submissions for Integer to
* Roman.
*
* Memory Usage: 9.2 MB, less than 11.00% of C++ online submissions for Integer
* to Roman.
*/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
string intToRoman(int num) {
if (num <= 0) return "";
vector<string> numerals{
"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I",
};
vector<int> weights{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
string result = "";
for (int i = 0; i < weights.size() && num != 0; i++) {
for (int j = 1; j <= num / weights[i]; j++) result += numerals[i];
num %= weights[i];
}
return result;
}
};
JavaScript
/*
* Runtime: 176 ms, faster than 33.68% of JavaScript online submissions for Integer to Roman.
*
* Memory Usage: 45.2 MB, less than 6.38% of JavaScript online submissions for Integer to Roman.
*/
const intToRoman = (num) => {
const numerals = [
"M",
"CM",
"D",
"CD",
"C",
"XC",
"L",
"XL",
"X",
"IX",
"V",
"IV",
"I",
];
const weights = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
let result = "";
for (let i = 0; i < weights.length && num !== 0; i++) {
for (let j = 1; j <= num / weights[i]; j++) result += numerals[i];
num %= weights[i];
}
return result;
};
1
/*
* Runtime: 168 ms, faster than 53.26% of JavaScript online submissions for Integer to Roman.
*
* Memory Usage: 45.5 MB, less than 6.38% of JavaScript online submissions for Integer to Roman.
*/
const intToRoman = (num) => {
const thousands = ["", "M", "MM", "MMM"];
const hundreds = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
const tens = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
const units = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
const val1 = num % 1000;
const val2 = val1 % 100;
const val3 = val2 % 10;
return (
thousands[(num - val1) / 1000] +
hundreds[(val1 - val2) / 100] +
tens[(val2 - val3) / 10] +
units[val3 % 10]
);
};
TypeScript
/*
* Runtime: 160 ms, faster than 77.17% of TypeScript online submissions for Integer to Roman.
*
* Memory Usage: 46.6 MB, less than 19.57% of TypeScript online submissions for Integer to Roman.
*/
const intToRoman = (num: number): string => {
const numerals = [
"M",
"CM",
"D",
"CD",
"C",
"XC",
"L",
"XL",
"X",
"IX",
"V",
"IV",
"I",
];
const weights = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
let result = "";
for (let i = 0; i < weights.length && num !== 0; i++) {
for (let j = 1; j <= num / weights[i]; j++) result += numerals[i];
num %= weights[i];
}
return result;
};
1
/*
* Runtime: 172 ms, faster than 43.48% of TypeScript online submissions for Integer to Roman.
*
* Memory Usage: 46.1 MB, less than 19.57% of TypeScript online submissions for Integer to Roman.
*/
const intToRoman = (num: number): string => {
const thousands = ["", "M", "MM", "MMM"];
const hundreds = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
const tens = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
const units = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
const val1 = num % 1000;
const val2 = val1 % 100;
const val3 = val2 % 10;
return (
thousands[(num - val1) / 1000] +
hundreds[(val1 - val2) / 100] +
tens[(val2 - val3) / 10] +
units[val3 % 10]
);
};