ローマ数字を表す文字列 s が与えられた場合、それに対応する整数値を見つけます。
ローマ数字は次の記号を使用して形成されます: I = 1 V = 5 X = 10 L = 50 C = 100 D = 500 および M = 1000。
通常、数値はこれらの記号を左から右に組み合わせ、特定のルールに基づいて値を加算または減算することによって形成されます。
ジャッカル対オオカミ
変換はどのように行われますか?
- より小さい値の記号が減算の前に来る場合。それ以外の場合は追加します。
- IV では、I は V の前にあり、V の値は 5 より大きくなります。したがって、結果は 5 - 1 = 4 となります。
- VI では、V は I の前にあり、I の値は 1 より小さいため、結果は 5 + 1 = 6 となります。
- II では同じ値があるため、加算して 1 + 1 = 2 を取得します。
- 3 文字以上の場合は、左から右に移動し、小さい値の文字の後に大きい値の文字が見つかった場合にのみグループ化します。たとえば、MXVII は 1000 + 10 + 5 + 1 + 1 = 1017 です。XLVII は (50 - 10) + 5 + 1 + 1 = 47 です。L の方が大きく、X の後に来ることに注意してください。
例:
入力: s = 'IX'
出力: 9
説明: IX は 10 - 1 = 9 を表すローマ記号です。入力: s = 'XL'
出力: 40
説明: XL は 50 - 10 = 40 を表すローマ記号です。すべて大文字のコマンド Excel入力: s = 'MCIV'
出力: 1904年
説明: M は 1000、CM は 1000 - 100 = 900、IV は 4 です。したがって、合計は 1000 + 900 + 4 = 1904 となります。
目次
[想定されるアプローチ 1] 反復比較の使用 - O(n) 時間と O(1) 空間
C++ローマ数字を整数に変換するための考え方は、文字列を左から右にトラバースする必要があるということです。各シンボルについて、次のシンボル (存在する場合) と比較します。現在のシンボルが次のシンボル以上である場合は、その値を結果に加算します。それ以外の場合は、次のシンボルの値からその値を減算し、その結果を合計に加算し、次のシンボルをスキップします。
#include using namespace std; // this function returns value of a Roman symbol int value(char r) { if (r == 'I') return 1; if (r == 'V') return 5; if (r == 'X') return 10; if (r == 'L') return 50; if (r == 'C') return 100; if (r == 'D') return 500; if (r == 'M') return 1000; return -1; } // returns decimal value of roman numeral int romanToDecimal(string& s) { int res = 0; for (int i = 0; i < s.length(); i++) { // get value of current symbol int s1 = value(s[i]); // Compare with the next symbol if it exists if (i + 1 < s.length()) { int s2 = value(s[i + 1]); // if current value is greater or equal // add it to result if (s1 >= s2) { res += s1; } else { // else add the difference and skip // next symbol res += (s2 - s1); i++; } } else { res += s1; } } return res; } int main() { string s = 'IX'; cout << romanToDecimal(s) << endl; return 0; }
Java class GfG { // this function returns value of a Roman symbol static int value(char r) { if (r == 'I') return 1; if (r == 'V') return 5; if (r == 'X') return 10; if (r == 'L') return 50; if (r == 'C') return 100; if (r == 'D') return 500; if (r == 'M') return 1000; return -1; } // returns decimal value of roman numeral static int romanToDecimal(String s) { int res = 0; for (int i = 0; i < s.length(); i++) { //get value of current symbol int s1 = value(s.charAt(i)); // compare with the next symbol if it exists if (i + 1 < s.length()) { int s2 = value(s.charAt(i + 1)); // If current value is greater or equal // add it to result if (s1 >= s2) { res += s1; } else { // else add the difference and skip // next symbol res += (s2 - s1); i++; } } else { res += s1; } } return res; } public static void main(String[] args) { String s = 'IX'; System.out.println(romanToDecimal(s)); } }
Python # this function returns value of a Roman symbol def value(r): if r == 'I': return 1 if r == 'V': return 5 if r == 'X': return 10 if r == 'L': return 50 if r == 'C': return 100 if r == 'D': return 500 if r == 'M': return 1000 return -1 # returns decimal value of roman numeral def romanToDecimal(s): res = 0 i = 0 while i < len(s): # get value of current symbol s1 = value(s[i]) # compare with the next symbol if it exists if i + 1 < len(s): s2 = value(s[i + 1]) # if current value is greater or equal # add it to result if s1 >= s2: res += s1 else: # else add the difference and # skip next symbol res += (s2 - s1) i += 1 else: res += s1 i += 1 return res if __name__ == '__main__': s = 'IX' print(romanToDecimal(s))
C# using System; class GfG { // this function returns value of a Roman symbol static int value(char r) { if (r == 'I') return 1; if (r == 'V') return 5; if (r == 'X') return 10; if (r == 'L') return 50; if (r == 'C') return 100; if (r == 'D') return 500; if (r == 'M') return 1000; return -1; } // returns decimal value of roman numeral static int romanToDecimal(string s) { int res = 0; for (int i = 0; i < s.Length; i++) { // get value of current symbol int s1 = value(s[i]); // compare with the next symbol if it exists if (i + 1 < s.Length) { int s2 = value(s[i + 1]); // if current value is greater or equal // add it to result if (s1 >= s2) { res += s1; } else { // else add the difference and skip // next symbol res += (s2 - s1); i++; } } else { res += s1; } } return res; } static void Main() { string s = 'IX'; Console.WriteLine(romanToDecimal(s)); } }
JavaScript // this function returns value of a Roman symbol function value(r) { if (r === 'I') return 1; if (r === 'V') return 5; if (r === 'X') return 10; if (r === 'L') return 50; if (r === 'C') return 100; if (r === 'D') return 500; if (r === 'M') return 1000; return -1; } // returns decimal value of roman numeral function romanToDecimal(s) { let res = 0; for (let i = 0; i < s.length; i++) { // get value of current symbol let s1 = value(s[i]); // compare with the next symbol if it exists if (i + 1 < s.length) { let s2 = value(s[i + 1]); // if current value is greater or equal // add it to result if (s1 >= s2) { res += s1; } else { // else add the difference and // skip next symbol res += (s2 - s1); i++; } } else { res += s1; } } return res; } // Driver Code let s = 'IX'; console.log(romanToDecimal(s));
出力
9
[想定されるアプローチ 2] ハッシュを使用する - O(n) 時間と O(1) 空間
C++ハッシュ マップまたは辞書を使用してローマ字記号の値を保存できます。そして、問題を解決するには、文字列を反復処理し、シンボルごとに現在の値が次の値より小さいかどうかを確認する必要があります。その場合は、次の値から現在の値を減算し、その結果を合計に加算します。それ以外の場合は、現在の値を合計に加算します。
Java Web サービス
#include #include using namespace std; int romanToDecimal(string &s) { unordered_map<char int> romanMap = {{'I' 1} {'V' 5} {'X' 10} {'L' 50} {'C' 100} {'D' 500} {'M' 1000}}; int res = 0; for (int i = 0; i < s.length(); i++) { // if the current value is less than the next value // subtract current from next and add to res if (i + 1 < s.length() && romanMap[s[i]] < romanMap[s[i + 1]]) { res += romanMap[s[i + 1]] - romanMap[s[i]]; // skip the next symbol i++; } else { // otherwise add the current value to res res += romanMap[s[i]]; } } return res; } int main() { string s = 'IX'; cout << romanToDecimal(s) << endl; return 0; }
Java import java.util.HashMap; class GfG { static int romanToDecimal(String s) { HashMap<Character Integer> romanMap = new HashMap<>(); romanMap.put('I' 1); romanMap.put('V' 5); romanMap.put('X' 10); romanMap.put('L' 50); romanMap.put('C' 100); romanMap.put('D' 500); romanMap.put('M' 1000); int res = 0; for (int i = 0; i < s.length(); i++) { // if the current value is less than the next value // subtract current from next and add to res if (i + 1 < s.length() && romanMap.get(s.charAt(i)) < romanMap.get(s.charAt(i + 1))) { res += romanMap.get(s.charAt(i + 1)) - romanMap.get(s.charAt(i)); // skip the next symbol i++; } else { // otherwise add the current value to res res += romanMap.get(s.charAt(i)); } } return res; } public static void main(String[] args) { String s = 'IX'; System.out.println(romanToDecimal(s)); } }
Python def romanToDecimal(s): romanMap = {'I': 1 'V': 5 'X': 10 'L': 50 'C': 100 'D': 500 'M': 1000} res = 0 i = 0 while i < len(s): # if the current value is less than the next value # subtract current from next and add to res if i + 1 < len(s) and romanMap[s[i]] < romanMap[s[i + 1]]: res += romanMap[s[i + 1]] - romanMap[s[i]] # skip the next symbol i += 1 else: # otherwise add the current value to res res += romanMap[s[i]] i += 1 return res if __name__ == '__main__': s = 'IX' print(romanToDecimal(s))
C# using System; using System.Collections.Generic; class GfG { static int romanToDecimal(string s) { // create a map to store the Roman numeral values Dictionary<char int> romanMap = new Dictionary<char int> { {'I' 1} {'V' 5} {'X' 10} {'L' 50} {'C' 100} {'D' 500} {'M' 1000} }; int res = 0; for (int i = 0; i < s.Length; i++) { // if the current value is less than the next value // subtract current from next and add to res if (i + 1 < s.Length && romanMap[s[i]] < romanMap[s[i + 1]]) { res += romanMap[s[i + 1]] - romanMap[s[i]]; // skip the next symbol i++; } else { // otherwise add the current value to res res += romanMap[s[i]]; } } return res; } static void Main() { string s = 'IX'; Console.WriteLine(romanToDecimal(s)); } }
JavaScript function romanToDecimal(s) { // create a map to store the Roman numeral values const romanMap = { 'I': 1 'V': 5 'X': 10 'L': 50 'C': 100 'D': 500 'M': 1000 }; let res = 0; for (let i = 0; i < s.length; i++) { // if the current value is less than the next value // subtract current from next and add to res if (i + 1 < s.length && romanMap[s[i]] < romanMap[s[i + 1]]) { res += romanMap[s[i + 1]] - romanMap[s[i]]; // skip the next symbol i++; } else { // otherwise add the current value to res res += romanMap[s[i]]; } } return res; } // Driver Code let s = 'IX'; console.log(romanToDecimal(s));
出力
9