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 };
  • 相关阅读:
    Error: bzip2: Compressed file ends unexpectedly; # perhaps it is corrupted?
    诡异shellbash脚本没写错运行的时候不报错也不执行
    seeksv
    常用Linux对脚本的基本操作——持续更新
    lumpy-sv
    常用linux对系统的基本操作——持续更新
    常用linux对文件的基本操作——持续更新
    css浮动与定位
    CSS知识点概要
    HTML5新手入门基础知识要点概要
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/5918545.html
Copyright © 2011-2022 走看看