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 };
  • 相关阅读:
    golang reflect知识集锦
    go test benchmark
    go build -tags 的使用
    golang http 服务器的接口梳理
    go中如何更好的迭代
    滚动条css实现
    记vue nextTick用到的地方
    捕获Ctrl + C中断 优雅的退出程序 golang
    如何处理动态JSON in Go
    golang实现参数可变的技巧
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5918545.html
Copyright © 2011-2022 走看看