• Roman to Integer


    问题描述

    Given a roman numeral, convert it to an integer.

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

    算法

    代码一:

     1 public int romanToInt(String s) {
     2         int ret = 0;
     3         char c, c1;
     4         for (int i = s.length() - 1; i >= 0; i--) {
     5             if (i == s.length() - 1) {
     6                 c = s.charAt(i);
     7                 if (c == 'I') {
     8                     ret += 1;
     9                 } else if (c == 'V') {
    10                     ret += 5;
    11                 } else if (c == 'X') {
    12                     ret += 10;
    13                 } else if (c == 'L') {
    14                     ret += 50;
    15                 } else if (c == 'C') {
    16                     ret += 100;
    17                 } else if (c == 'D') {
    18                     ret += 500;
    19                 } else if (c == 'M') {
    20                     ret += 1000;
    21                 }
    22                 continue;
    23             }
    24 
    25             c = s.charAt(i);
    26             c1 = s.charAt(i + 1);
    27             if (c == 'I') {
    28                 if (c1 == 'V' || c1 == 'X') {
    29                     ret -= 1;
    30                 } else {
    31                     ret += 1;
    32                 }
    33             } else if (c == 'V') {
    34                 ret += 5;
    35             } else if (c == 'X') {
    36                 if (c1 == 'L' || c1 == 'C') {
    37                     ret -= 10;
    38                 } else {
    39                     ret += 10;
    40                 }
    41             } else if (c == 'L') {
    42 
    43                 ret += 50;
    44 
    45             } else if (c == 'C') {
    46                 if (c1 == 'D' || c1 == 'M') {
    47                     ret -= 100;
    48                 } else {
    49                     ret += 100;
    50                 }
    51             } else if (c == 'D') {
    52 
    53                 ret += 500;
    54 
    55             } else if (c == 'M') {
    56                 ret += 1000;
    57             }
    58         }
    59 
    60         return ret;
    61     }

    代码二:

     1     public int romanToInt(String s) {
     2         Hashtable<Character,Integer> value;
     3         value=new Hashtable<Character,Integer>();
     4         value.put('M',1000);
     5         value.put('D',500);
     6         value.put('C',100);
     7         value.put('L',50);
     8         value.put('X',10);
     9         value.put('V',5);
    10         value.put('I',1);
    11         int sum=0;
    12         for(int i=0;i<s.length();i++)
    13         {
    14             if(i<s.length()-1&&value.get(s.charAt(i))<value.get(s.charAt(i+1)))
    15                 sum-=value.get(s.charAt(i));
    16             else
    17                 sum+=value.get(s.charAt(i));
    18         }
    19         return sum;
    20     }

    代码三:

     1 public int romanToInt(String str) {
     2     int[] a = new int[26];
     3     a['I' - 'A'] = 1;
     4     a['V' - 'A'] = 5;
     5     a['X' - 'A'] = 10;
     6     a['L' - 'A'] = 50;
     7     a['C' - 'A'] = 100;
     8     a['D' - 'A'] = 500;
     9     a['M' - 'A'] = 1000;
    10     char prev = 'A';
    11     int sum = 0;
    12     for(char s : str.toCharArray()) {
    13         if(a[s - 'A'] > a[prev - 'A']) {
    14             sum = sum - 2 * a[prev - 'A'];
    15         }
    16         sum = sum + a[s - 'A'];
    17         prev = s;
    18     }
    19     return sum;
    20 }

    代码四:

     1 public int romanToInt(String s) {
     2     int nums[]=new int[s.length()];
     3     for(int i=0;i<s.length();i++){
     4         switch (s.charAt(i)){
     5             case 'M':
     6                 nums[i]=1000;
     7                 break;
     8             case 'D':
     9                 nums[i]=500;
    10                 break;
    11             case 'C':
    12                 nums[i]=100;
    13                 break;
    14             case 'L':
    15                 nums[i]=50;
    16                 break;
    17             case 'X' :
    18                 nums[i]=10;
    19                 break;
    20             case 'V':
    21                 nums[i]=5;
    22                 break;
    23             case 'I':
    24                 nums[i]=1;
    25                 break;
    26         }
    27     }
    28     int sum=0;
    29     for(int i=0;i<nums.length-1;i++){
    30         if(nums[i]<nums[i+1])
    31             sum-=nums[i];
    32         else
    33             sum+=nums[i];
    34     }
    35     return sum+nums[nums.length-1];
    36 }

    注意事项

    1.罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000)。按照下面的规则可以表示任意正整数。

    重复数次:一个罗马数字重复几次,就表示这个数的几倍。
    右加左减:在一个较大的罗马数字的右边记上一个较小的罗马数字,表示大数字加小数字。在一个较大的数字的左边记上一个较小的罗马数字,表示大数字减小数字。但是,左减不能跨越等级。比如,99不可以用IC表示,用XCIX表示。
    加线乘千:在一个罗马数字的上方加上一条横线或者在右下方写M,表示将这个数字乘以1000,即是原数的1000倍。同理,如果上方有两条横线,即是原数的1000000倍。
    单位限制:同样单位只能出现3次,如40不能表示为XXXX,而要表示为XL
    2.对于字符串s="qwert",s[0]是q而不是t。

  • 相关阅读:
    shell 如何避免误删目录
    Linux 禁止用户或 IP通过 SSH 登录
    gitlab不能启动了
    清空分区表里某个分区的数据
    mysql错误Table ‘./mysql/proc’ is marked as crashed and should be repaired
    MySQL Server参数优化
    linux下删除乱码文件、目录
    ERROR 1044 (42000): Access denied for user 'root'@'localhost'
    awk 打印从某一列到最后一列的内容
    连接和关闭资源工具类
  • 原文地址:https://www.cnblogs.com/qiaoshanzi/p/4936959.html
Copyright © 2020-2023  润新知