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

    • Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
      Symbol Value
      I 1
      V 5
      X 10
      L 50
      C 100
      D 500
      M 1000

    • For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

    • Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

    1. I can be placed before V (5) and X (10) to make 4 and 9.
    2. X can be placed before L (50) and C (100) to make 40 and 90.
    3. C can be placed before D (500) and M (1000) to make 400 and 900.
    4. Given a roman numeral, convert it to an integer.

    Example 1:

    Input: s = "III"
    Output: 3

    Example 2:

    Input: s = "IV"
    Output: 4

    Example 3:

    Input: s = "IX"
    Output: 9

    Example 4:

    Input: s = "LVIII"
    Output: 58
    Explanation: L = 50, V= 5, III = 3.

    Example 5:

    Input: s = "MCMXCIV"
    Output: 1994
    Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

    Constraints:

    • 1 <= s.length <= 15
    • s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
    • It is guaranteed that s is a valid roman numeral in the range [1, 3999].
    public class Solution {
        public int RomanToInt(string s) {
            
        }
    }
    

    方法1

    using System;
    
    public class Solution {    
        //获取最后的总和
        public int RomanToInt(string s) { 
            int sum = 0, i;     
            for(i = 0; i < s.Length - 1 ; i++){            
                if(Compare(s[i], s[i+1]) > 0){                
                    sum += Compare(s[i], s[i+1]);                
                    ++i;
                }else{
                    sum += GetSzize(s[i]);                           
                }                          
            }    
            //把最后的一个元素的值加到sum中
            if(i == s.Length - 1){                                       
                sum =  sum + GetSzize(s[s.Length - 1]);
            }
            return sum;    
        }   
        //前后两个字符相比
        public int Compare(char front, char behind){
            if(GetSzize(front) < GetSzize(behind)){
                return (GetSzize(behind) - GetSzize(front));
            }
            return 0;
        }
        //获取字符的大小
        public int GetSzize(char word){
             switch(word){
                case 'I':
                    return 1;
                case 'V':
                    return 5;
                case 'X':
                    return 10;
                case 'L':
                    return 50;
                case 'C':
                    return 100;      
                case 'D':
                    return 500; 
                case 'M':
                    return 1000;             
            }    
            return 0;
        }
        //判断定义的字符串是否合理
        public bool Constraints(string s){
            if(s.Length <= 0 || s.Length > 15){
                return false;
            }
            foreach(int element in s){
                switch(element){
                    case 'I':
                        continue;
                    case 'V':
                        continue;
                    case 'X':
                        continue;                   
                    case 'L':
                        continue;
                    case 'C':
                        continue;       
                    case 'D':
                        continue;   
                    case 'M':
                        continue;   
                    default:
                        return false;   
                } 
            }
            return true;
        }
        static void Main(){
            Solution so = new Solution();
            string s = "LVIII";
            Console.WriteLine("字符串的合理性:" + so.Constraints(s));
            Console.WriteLine("sum=" + so.RomanToInt(s));
        }
    }
    

    方法2

    
    /*
    struct Words{
        pulic string word;
        pulic int size;
    };*/
     /* Words w1;
            Words w2;
            Words w3;
            Words w4;
            Words w5;
            Words w6;
            Words w7;
            w1.word = 'I'
            w1.size =  1;
            w2.word = 'V'
            w2.size =  5;
            w3.word = 'X'
            w3.size =  10;
            w4.word = 'L'
            w4.size =  50;
            w5.word = 'C'
            w5.size =  100;
            w6.word = 'D'
            w6.size =  500;
            w7.word = 'M'
            w7.size =  1000;
            Console.WriteLine("{0},{1}",w1.Word , w1.size);
     */     
    

    ❤️有则改之,无则加勉。如有错误、建议、疑问,评论或联系飞沙QQ:2602629646
    ❤️本文来自作者:MrFlySand,转载请注明原文链接:https://www.cnblogs.com/MrFlySand/p/15390300.html

  • 相关阅读:
    Java8中利用stream对map集合进行过滤的方法
    安装数据库MySQL,启动时报错 服务没有响应控制功能 的解决办法
    mysql 安装时 失败,提示 因为计算机中丢失 msvcp140.dll
    复习一下数学排列组合公式的原理
    java如何进行排列组合运算
    Redis 分布式锁:使用Set+lua替代 setnx
    深入详解Go的channel底层实现原理【图解】
    MYSQL MVCC实现原理详解
    聚簇索引和非聚簇索引,全在这!!!
    深度解密Go语言之 map
  • 原文地址:https://www.cnblogs.com/MrFlySand/p/15390300.html
Copyright © 2011-2022 走看看