王廷瑋|數位醫療|智慧醫療: 12. Integer to Roman WFU

2024年7月2日 星期二

12. Integer to Roman

12. Integer to Roman


七個不同的符號代表羅馬數字,具有以下值:

符號 數值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000

羅馬數字是通過從最高到最低依次附加十進位值的轉換來形成的。將十進位值轉換為羅馬數字具有以下規則:如果數值不以 4 或 9 開頭,選擇可以從輸入中減去的最大值的符號,將該符號附加到結果中,減去該符號的值,並將剩餘部分轉換為羅馬數字。
如果數值以 4 或 9 開頭,使用減法形式表示一個符號減去後一個符號,例如,4 是比 5(V)少 1(I):IV,9 是比 10(X)少 1(I):IX。僅使用以下減法形式:4(IV),9(IX),40(XL),90(XC),400(CD)和 900(CM)。
只有 10 的冪次(I,X,C,M)可以連續附加最多 3 次來表示 10 的倍數。你不能多次附加 5(V),50(L)或 500(D)。如果需要附加某個符號 4 次,請使用減法形式。

給定一個整數,將其轉換為羅馬數字。

範例 1:

輸入:num = 3749

輸出:"MMMDCCXLIX"

解釋:

3000 = MMM 為 1000(M) + 1000(M) + 1000(M) 700 = DCC 為 500(D) + 100(C) + 100(C) 40 = XL 為 10(X)比 50(L)少 9 = IX 為 1(I)比 10(X)少 注意:49 不是 1(I)比 50(L)少,因為轉換是基於十進位的。


Python


class Solution:
def intToRoman(self, num: int) -> str:
# Define the mapping of values to Roman numerals
value_map = [
(1000, "M"),
(900, "CM"),
(500, "D"),
(400, "CD"),
(100, "C"),
(90, "XC"),
(50, "L"),
(40, "XL"),
(10, "X"),
(9, "IX"),
(5, "V"),
(4, "IV"),
(1, "I")
]
# Initialize the result string
result = []
# Iterate through the value_map to construct the Roman numeral
for value, symbol in value_map:
# While the current value can be subtracted from num, append the
                corresponding symbol
while num >= value:
result.append(symbol)
num -= value
# Join and return the result list as a string
return ''.join(result)

16.63MB, 53ms


C++


#include <string>
#include <vector>

using namespace std;

class Solution {
public:
string intToRoman(int num) {
// Define the mapping of values to Roman numerals
vector<pair<int, string>> value_map = {
{1000, "M"},
{900, "CM"},
{500, "D"},
{400, "CD"},
{100, "C"},
{90, "XC"},
{50, "L"},
{40, "XL"},
{10, "X"},
{9, "IX"},
{5, "V"},
{4, "IV"},
{1, "I"}
};

// Initialize the result string
string result;

// Iterate through the value_map to construct the Roman numeral
for (const auto& [value, symbol] : value_map) {
// While the current value can be subtracted from num, append the
                corresponding symbol
while (num >= value) {
result += symbol;
num -= value;
}
}

// Return the result string
return result;
}
};

13.36MB, 15ms


Javascript

/**
* @param {number} num
* @return {string}
*/
var intToRoman = function(num) {
// Define the mapping of values to Roman numerals
const valueMap = [
{ value: 1000, symbol: "M" },
{ value: 900, symbol: "CM" },
{ value: 500, symbol: "D" },
{ value: 400, symbol: "CD" },
{ value: 100, symbol: "C" },
{ value: 90, symbol: "XC" },
{ value: 50, symbol: "L" },
{ value: 40, symbol: "XL" },
{ value: 10, symbol: "X" },
{ value: 9, symbol: "IX" },
{ value: 5, symbol: "V" },
{ value: 4, symbol: "IV" },
{ value: 1, symbol: "I" }
];

// Initialize the result string
let result = '';

// Iterate through the valueMap to construct the Roman numeral
for (const { value, symbol } of valueMap) {
// While the current value can be subtracted from num, append the
            corresponding symbol
while (num >= value) {
result += symbol;
num -= value;
}
}

// Return the result string
return result;
};

55.82MB, 85ms