zoukankan      html  css  js  c++  java
  • Roman to Integer

    Given a roman numeral, convert it to an integer.

    Input is guaranteed to be within the range from 1 to 3999.

    Runtime: 59ms

     1 class Solution {
     2 public:
     3     int romanToInt(string s) {
     4         if (s.empty()) return 0;
     5         
     6         int result = 0;
     7         int n = s.size();
     8         unordered_map<char, int> roman;
     9         roman['I'] = 1;
    10         roman['V'] = 5;
    11         roman['X'] = 10;
    12         roman['L'] = 50;
    13         roman['C'] = 100;
    14         roman['D'] = 500;
    15         roman['M'] = 1000;
    16         
    17         for (int i = 0; i < n; i++) {
    18             if (i < n - 1) {
    19                 if (s[i] == 'I' && s[i + 1] == 'V') {result += 4; i++;}
    20                 else if (s[i] == 'I' && s[i + 1] == 'X') {result += 9; i++;}
    21                 else if (s[i] == 'X' && s[i + 1] == 'L') {result += 40; i++;}
    22                 else if (s[i] == 'X' && s[i + 1] == 'C') {result += 90; i++;}
    23                 else if (s[i] == 'C' && s[i + 1] == 'D') {result += 400; i++;}
    24                 else if (s[i] == 'C' && s[i + 1] == 'M') {result += 900; i++;}
    25                 else result += roman[s[i]];
    26             }
    27             else {
    28                 result += roman[s[i]];
    29             }
    30         }
    31         return result;
    32     }
    33 };

    Runtime: 92ms

     1 class Solution {
     2 public:
     3     int romanToInt(string s) {
     4         if (s.empty()) return 0;
     5         
     6         int result = 0;
     7         int n = s.size();
     8         unordered_map<string, int> roman;
     9         roman["I"] = 1;
    10         roman["V"] = 5;
    11         roman["X"] = 10;
    12         roman["L"] = 50;
    13         roman["C"] = 100;
    14         roman["D"] = 500;
    15         roman["M"] = 1000;
    16         roman["IV"] = 4;
    17         roman["IX"] = 9;
    18         roman["XL"] = 40;
    19         roman["XC"] = 90;
    20         roman["CD"] = 400;
    21         roman["CM"] = 900;
    22         
    23         for (int i = 0; i < n;) {
    24             if (i < n - 1) {
    25                 string temp = s.substr(i, 2);
    26                 if (roman.find(temp) != roman.end()) {
    27                     result += roman[temp];
    28                     i += 2;
    29                     continue;
    30                 }
    31             }
    32             result += roman[s.substr(i, 1)];
    33             i++;
    34         }
    35         return result;
    36     }
    37 };
  • 相关阅读:
    [WC2010]重建计划
    [Codeforces150E] Freezing with Style
    [Codeforces915F] Imbalance Value of a Tree
    [Codeforces1055F] Tree and XOR
    [Codeforces1117G]Recursive Queries
    [Codeforces587F]Duff is Mad
    [Codeforces547E]Mike and Friends
    [2020团体程序设计天梯赛-总决赛L3-2] 传送门
    第05组 Beta冲刺 (1/5)
    第05组 Alpha冲刺 总结
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5918545.html
Copyright © 2011-2022 走看看